2010-08-09 69 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>