1

配置文件中是否有可以在 EntLib 中打开或关闭日志记录的设置。有帖子展示了如何以编程方式执行此操作,但我有兴趣通过配置文件来执行此操作。

我正在使用 EntLib 5。

这是配置

 <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
        <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            source="Enterprise Library Logging" formatter="Text Formatter"
            log="" machineName="." traceOutputOptions="None" />
        <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            fileName="c:\\Log\\wclog.log" header=""
            formatter="Text Formatter" traceOutputOptions="DateTime, Timestamp" />
    </listeners>
    <formatters>
        <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            template="Timestamp:{timestamp}  Method: {title}, {category} {message}"
            name="Text Formatter" />
    </formatters>
    <categorySources>
        <add switchValue="All" name="General">
            <listeners>
                <add name="Flat File Trace Listener" />
            </listeners>
        </add>
    </categorySources>
    <specialSources>
        <allEvents switchValue="All" name="All Events">
            <listeners>
                <add name="Flat File Trace Listener" />
            </listeners>
        </allEvents>
        <notProcessed switchValue="All" name="Unprocessed Category">
            <listeners>
                <add name="Flat File Trace Listener" />
            </listeners>
        </notProcessed>
        <errors switchValue="All" name="Logging Errors &amp; Warnings">
            <listeners>
                <add name="Flat File Trace Listener" />
            </listeners>
        </errors>
    </specialSources>

问候。

4

3 回答 3

5

有多种方法可以禁用日志记录。如前所述,将类别开关值设置为“关闭”:

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
        <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            source="Enterprise Library Logging" formatter="Text Formatter"
            log="" machineName="." traceOutputOptions="None" />
        <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            fileName="c:\\Log\\wclog.log" header=""
            formatter="Text Formatter" traceOutputOptions="DateTime, Timestamp" />
    </listeners>
    <formatters>
        <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            template="Timestamp:{timestamp}  Method: {title}, {category} {message}"
            name="Text Formatter" />
    </formatters>
    <categorySources>
        <add switchValue="All" name="General">
            <listeners>
                <add name="Flat File Trace Listener" />
            </listeners>
        </add>
    </categorySources>
    <specialSources>
        <allEvents switchValue="All" name="All Events">
            <listeners>
                <add name="Flat File Trace Listener" />
            </listeners>
        </allEvents>
        <notProcessed switchValue="All" name="Unprocessed Category">
            <listeners>
                <add name="Flat File Trace Listener" />
            </listeners>
        </notProcessed>
        <errors switchValue="All" name="Logging Errors &amp; Warnings">
            <listeners>
                <add name="Flat File Trace Listener" />
            </listeners>
        </errors>
    </specialSources>

您还可以通过将跟踪侦听器本身的过滤器属性设置为“关闭”来禁用单个跟踪侦听器。

全局禁用日志记录的最简单方法可能是使用 Logging Enabled Filter:

    <logFilters>
        <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            enabled="false" name="Logging Enabled Filter" />
    </logFilters>

然后只需设置“true”或“false”来启用或禁用日志记录。过滤器还具有在“日志管道”中首先执行的好处,因此它应该比将单个跟踪侦听器设置为“关闭”具有更好的性能。

于 2012-08-10T05:39:50.783 回答
2

虽然不理想,但这是一个应该可行的简单方法 - 注释掉听众:

<categorySources>
    <add switchValue="All" name="General">
        <listeners>
            <!-- <add name="Flat File Trace Listener" /> -->
        </listeners>
    </add>
</categorySources>
<specialSources>
    <allEvents switchValue="All" name="All Events">
        <listeners>
           <!--  <add name="Flat File Trace Listener" />  -->
        </listeners>
    </allEvents>
    <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
           <!--  <add name="Flat File Trace Listener" />  -->
        </listeners>
    </notProcessed>
    <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <!--   <add name="Flat File Trace Listener" />  -->
        </listeners>
    </errors>
</specialSources>
于 2012-08-01T15:06:20.937 回答
0

您可以将 categorySources 的 switchValue 设置为“关闭”

于 2012-07-25T07:54:51.233 回答