【问题标题】:How to write just message to debug output with Enterprise Library Logging?如何使用 Enterprise Library Logging 仅编写消息以调试输出?
【发布时间】:2009-06-18 06:36:08
【问题描述】:

我想使用 EntLib Logging 实现日志记录,并为“调试”类别连接两个 TraceListener。一个将这些消息写入文件,另一个将它们输出到系统跟踪输出,就像 Debug.Write 一样(这样我就可以使用 Sysinternals DbgView 监视它们),但我找不到如何使用格式化程序设置第二个侦听器我需要。我真正需要的只是消息,但它会输出一大堆东西,比如 EventId、Priority 等。我该如何删掉所有这些东西?

【问题讨论】:

    标签: logging enterprise-library


    【解决方案1】:

    我在 MSDN 上找到了一个不错的演练:Creating a Custom Trace Listener

    它完全符合我的需要。这是我最终得到的完整代码:

    using System;
    using System.Diagnostics;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Logging;
    using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
    
    namespace Common.Utils
    {
        [ConfigurationElementType(typeof(CustomTraceListenerData))]
        public class FormattedDebugWriterTraceListener : CustomTraceListener
        {
            public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
            {
                if (data is LogEntry && this.Formatter != null)
                {
                    this.WriteLine(this.Formatter.Format(data as LogEntry));
                }
                else
                {
                    this.WriteLine(data.ToString());
                }
            }
    
            public override void Write(string message)
            {
                Debug.Write(message);
            }
    
            public override void WriteLine(string message)
            {
                Debug.WriteLine(message);
            }
    
        }
    }
    

    配置文件:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      </configSections>
      <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
        defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
        <listeners>
          <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            traceOutputOptions="None" type="Common.Utils.FormattedDebugWriterTraceListener, Common.Utils"
            name="FormattedDebugWriterTraceListener" initializeData="" formatter="SimpleMessageFormatter" />
          <add fileName="log\Debugging.log" rollSizeKB="0" timeStampPattern="yyyy-MM-dd"
            rollFileExistsBehavior="Overwrite" rollInterval="Week" formatter="GeneralTextFormatter"
            header="----------------------------------------" footer="----------------------------------------"
            listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            name="RollingFlatFileTraceListener" />
        </listeners>
        <formatters>
          <add template="{message}&#xD;&#xA;" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            name="SimpleMessageFormatter" />
          <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
            type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            name="GeneralTextFormatter" />
        </formatters>
        <categorySources>
          <add switchValue="All" name="Debugging">
            <listeners>
              <add name="FormattedDebugWriterTraceListener" />
              <add name="RollingFlatFileTraceListener" />
            </listeners>
          </add>
          <add switchValue="All" name="General" />
        </categorySources>
        <specialSources>
          <allEvents switchValue="All" name="All Events" />
          <notProcessed switchValue="All" name="Unprocessed Category" />
          <errors switchValue="All" name="Logging Errors &amp; Warnings" />
        </specialSources>
      </loggingConfiguration>
    </configuration>
    

    用法是这样的:

    Debug.Write("Debug.Write test");
    Logger.Write("EntLib test", "Debugging");
    

    两者都以调试输出结束,可以通过 DbgView 轻松跟踪。

    【讨论】:

    • 非常感谢您包含 XML - 它极大地帮助了我完成演练中无法解决的问题! (我没有看到他们在哪里提到设置监听器数据类型属性,这让我抓狂)
    【解决方案2】:

    在您的应用程序的 EntLib 配置中,您指定要使用的 Formatter。默认格式化程序包括所有这些信息。要删除您不感兴趣的信息,请从您当前使用的 TextFormatter 中删除它们,或者创建一个包含所需字段的新文本格式化程序,然后更改“调试”以使用您的新格式化程序。

    【讨论】:

    • 这正是我所做的,但看起来 DefaultTraceListener 不支持格式化程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多