c# - 在 EntLib 中登录/注销

标签 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>

问候。

最佳答案

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

<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>

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

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

    <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”即可启用或禁用日志记录。过滤器还具有在“日志记录管道”中首先执行的优势,因此它应该比将单个跟踪监听器设置为“关闭”具有更好的性能。

关于c# - 在 EntLib 中登录/注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11644030/

相关文章:

c# - WCF、Unity、EntLib5、Linq-to-SQL 与 TDD 和 PI 的示例

c# - 将性能计数器与属性一起使用

c# - C# 中的 IXmlSerialized 字典没有 'Key'/'Value' 节点

c# - 将 IQueryable 作为参数传递

c# - 从 C# 上传 zip 文件到 S3

使用企业库在 azure 中记录异常

c# - 带有企业应用程序 block 的自定义跟踪监听器

c# - 将word文档另存为图片

c# - 部分类跨越程序集

.net - 使用 EntLib 日志记录应用程序 block 使用 auth/secure smtp 服务器发送电子邮件