2009-06-18 33 views
11

我想用EntLib Logging实现日志记录,并为类别“Debugging”挂接两个TraceListeners。一个会将这些消息写入文件,另一个会将它们输出到系统跟踪输出,与Debug.Write一样(这样我就可以使用Sysinternals DbgView监视它们),但是我找不到如何使用格式化程序设置第二个监听器我需要。我真正需要的仅仅是消息,但是它输出了大量的东西,比如EventId,Priority等等。我怎么把这些东西都剪掉了?如何使用Enterprise Library Logging将消息写入调试输出?

回答

15

我发现在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。

+0

非常感谢您使用XML - 它极大地帮助我解决了从演练中无法解决的问题! (我没有看到他们提到的设置侦听器数据类型属性的地方,这让我发疯) – GrahamMc 2016-04-08 13:52:14

0

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

+2

这正是我所做的,但看起来像DefaultTraceListener不支持格式化程序。 – bychkov 2009-06-18 15:22:54

相关问题