我想用EntLib Logging实现日志记录,并为类别“Debugging”挂钩两个TraceListeners。一个会将这些消息写入文件,其他人将它们以与Debug.Write相同的方式输出到系统跟踪输出(这样我就可以用Sysinternals DbgView监视它们),但是我找不到如何用格式化程序设置这个第二个监听器我需要。我真正需要的只是消息,但它输出了大量的东西,比如EventId,Priority等。我如何剪掉所有这些东西?
答案 0 :(得分: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}
" 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}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}"
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 & Warnings" />
</specialSources>
</loggingConfiguration>
</configuration>
用法如下:
Debug.Write("Debug.Write test");
Logger.Write("EntLib test", "Debugging");
两者最终都可以通过DbgView轻松追踪调试输出。
答案 1 :(得分:0)
在应用的EntLib配置中,您可以指定要使用的Formatter。默认格式化程序包含所有这些信息。要删除您不感兴趣的信息,请从当前使用的TextFormatter中删除它们,或者创建一个包含所需字段的新文本格式化程序,并更改“调试”以使用新的格式化程序。