在EntLib中登录/注销

Cod*_*elp 1 c# enterprise-library

配置文件中是否有一个设置可以在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>
Run Code Online (Sandbox Code Playgroud)

问候.

Ran*_*ica 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>
Run Code Online (Sandbox Code Playgroud)

您还可以通过将跟踪侦听器本身的filter属性设置为"Off"来禁用单个跟踪侦听器.

全局禁用日志记录的最简单方法可能是使用启用日志记录的过滤器:

    <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>
Run Code Online (Sandbox Code Playgroud)

然后只需设置"true"或"false"即可启用或禁用日志记录.过滤器还具有首先在"日志记录管道"中执行的优点,因此它应该比将各个跟踪侦听器设置为"关闭"具有更好的性能.