2009-06-18 31 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

相關問題