2010-08-09 70 views
4

我需要執行日誌記錄到控制檯(或調試/跟蹤)。目前我正在使用Ent Lib 4.1日誌記錄應用程序塊。要登錄到控制檯,請使用SystemDiagnosticsTraceListenerData和System.Diagnostics.ConsoleTraceListener。Logging Application Block的SystemDiagnosticsTraceListenerData監聽器的格式化日誌記錄

它執行日誌記錄到控制檯罰款,但我無法使用格式化程序的這種列表類型,因此不能格式化日誌條目到所需的格式。我需要的僅僅是日誌消息,而不需要默認提供的所有附加信息(這使得日誌對於我的情況的可讀性較差)。

是否有任何配置選項我缺少啓用SystemDiagnosticsTraceListener格式?

回答

0

我不知道這是否有幫助(因爲您正在使用LAB),但是Ukadc.Diagnostics是一組System.Diagnostics擴展。一個主要的補充(與System.Diagnostics相比)增加了格式化記錄輸出的功能,類似於Log4net,NLog和LAB可以完成的功能。您甚至可以通過編寫自己的「令牌」來擴展格式化功能。令牌是由Ukadc.Diagnostics提供的自定義TraceListeners調用的對象,用於獲取要記錄的信息(以及日誌消息本身)。例如,我編寫了一個對象來計算自處理啓動以來的時間增量,以毫秒爲單位。如果我在格式語句中包含相應的標記,則每個日誌消息將包含該增量。

請參見以下鏈接:

Ukadc.Diagnostics on codeplex

The developer's blog

8

一個做到這一點的方法是創建一個自定義的TraceListener。然後在app.config中將listnerDataType設置爲CustomTraceListenerData。這允許ConsoleTraceListener具有格式化輸出。輸出格式按預期格式化。沒有格式化程序,所有的值都被返回。自定義TraceListener和app.config都被附加。

該代碼使用5.0.505.0而不是原來的問題提出的4.1。

此代碼取自本網站:java2s firstbricks » FirstBricks » EntLib » Logging » Extensions » ConsoleTraceListener.cscache)。 刪除了評論以便使代碼更易於在StackOverflow上閱讀,並且默認顏色已從白色更改爲灰色。

namespace Eab.Logging 
{ 
    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; 

    [ConfigurationElementType(typeof(CustomTraceListenerData))] 
    public class ConsoleTraceListener : CustomTraceListener 
    { 
     public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) 
     { 
      if (data is LogEntry && Formatter != null) { 
       LogEntry le = (LogEntry)data; 
       WriteLine(Formatter.Format(le), le.Severity); 
      } else { 
       WriteLine(data.ToString()); 
      } 
     } 

     public override void Write(string message) 
     { 
      Console.ForegroundColor = ConsoleColor.Gray; 
      Console.Write(message); 
     } 

     public override void WriteLine(string message) 
     { 
      Console.ForegroundColor = ConsoleColor.Gray; 
      Console.WriteLine(message); 
     } 

     public void WriteLine(string message, TraceEventType severity) 
     { 
      ConsoleColor color; 
      switch (severity) { 
       case TraceEventType.Critical: 
       case TraceEventType.Error: 
        color = ConsoleColor.Red; 
        break; 
       case TraceEventType.Warning: 
        color = ConsoleColor.Yellow; 
        break; 
       case TraceEventType.Information: 
        color = ConsoleColor.Cyan; 
        break; 
       case TraceEventType.Verbose: 
       default: 
        color = ConsoleColor.Gray; 
        break; 
      } 

      Console.ForegroundColor = color; 
      Console.WriteLine(message); 
     } 
    } 
} 

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General"> 
    <listeners> 
       <!-- Namespace+class, applicationName -->  
     <add 
      type="Eab.Logging.ConsoleTraceListener, Eab.Logging" 
      listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
      formatter="Simple Formatter" 
      name="Console Listener" /> 
    </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(local:[MM/dd/yyyy HH:mm:ss.fff])} : ({title}) {message}" 
      name="Simple Formatter" /> 
    </formatters> 
    <categorySources> 
     <add switchValue="All" name="General"> 
     <listeners> 
      <add name="Console Listener" /> 
     </listeners> 
     </add> 
    </categorySources> 
    <specialSources> 
     <notProcessed switchValue="All" name="Unprocessed Category"> 
     <listeners> 
      <add name="Console Listener" /> 
     </listeners> 
     </notProcessed> 
     <errors switchValue="All" name="Logging Errors &amp; Warnings"> 
     <listeners> 
      <add name="Console Listener" /> 
     </listeners> 
     </errors> 
    </specialSources> 
    </loggingConfiguration>