未在EnterpriseLibrary日志记录的自定义跟踪侦听器中设置格式化程序
本文关键字:侦听器 设置 格式化 程序 跟踪 自定义 EnterpriseLibrary 日志 记录 未在 | 更新日期: 2023-09-27 18:22:13
我为EnterpriseLibrary日志记录块创建了一个自定义跟踪侦听器,但Formatter
属性始终为null。
这是代码:
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using System;
using System.Diagnostics;
namespace test {
[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class TestTraceListener : CustomTraceListener {
public override void Write(string message) {
Console.Write(message);
}
public override void WriteLine(string message) {
Console.WriteLine(message);
}
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) {
LogEntry entry = data as LogEntry;
if (entry != null) {
if (Formatter != null) {
string formatted = Formatter.Format(entry);
WriteLine(formatted);
} else {
WriteLine(entry.Message);
}
} else {
base.TraceData(eventCache, source, eventType, id, data);
}
}
}
class Program {
static void Main(string[] args) {
Logger.SetLogWriter(new LogWriterFactory().Create());
LogEntry entry = new LogEntry("This is a test", "General", 0, 0, TraceEventType.Information, null, null);
Logger.Write(entry);
}
}
}
这是配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="loggingConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
requirePermission="true" />
</configSections>
<loggingConfiguration name="logging" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Console Trace Listener"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.SystemDiagnosticsTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
formatter="Simple Formatter"
type="test.TestTraceListener, test"
traceOutputOptions="DateTime, Timestamp, ThreadId" />
</listeners>
<formatters>
<add name="Simple Formatter"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="{timestamp(local:dd/MM/yy HH:mm:ss.fff)} [{severity}]: {message}" />
</formatters>
<categorySources>
<add switchValue="Information" name="General">
<listeners>
<add name="Console Trace Listener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="Warning" name="Logging Errors & Warnings" />
</specialSources>
</loggingConfiguration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>
如果我理解正确,我的侦听器应该有我在配置文件中声明的"Simple Formatter"格式化程序的Formatter
属性,但事实并非如此。
我错过了什么?
我用企业库配置工具解决了这个问题(我可以按照以下说明使它适用于VS 2015:企业库6适用于Visual Studio 2013和/或2015吗?)。
问题是listenerDataType
属性的值错误,侦听器的XML声明应该如下所示:
<add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
type="test.TestTraceListener, test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Console Trace Listener"
formatter="Simple Formatter" />