Logging EnterpriseLibrary CustomTraceListener不尊重TextFormatter
我有一个CustomTraceListener,它记录到一个环形缓冲区:Logging EnterpriseLibrary CustomTraceListener不尊重TextFormatter,logging,enterprise-library,Logging,Enterprise Library,我有一个CustomTraceListener,它记录到一个环形缓冲区: namespace Sample { using System; using System.Collections.Generic; using System.Linq; using Cognoware.Collections.Generic; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
namespace Sample
{
using System;
using System.Collections.Generic;
using System.Linq;
using Cognoware.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class CircularTraceListener: CustomTraceListener
{
private static RingBuffer<string> _ringBuffer = new RingBuffer<string>(200);
private string tmp = String.Empty;
public override void Write(string message)
{
tmp += message;
}
public override void WriteLine(string message)
{
_ringBuffer.Add(tmp + message);
tmp = String.Empty;
}
public IEnumerable<string> Messages
{
get
{
return _ringBuffer.AsEnumerable<string>();
}
}
}
}
名称空间示例
{
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用Cognoware.Collections.Generic;
使用Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
使用Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
使用Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
[ConfigurationElementType(typeof(CustomTraceListenerData))]
公共类CircularTraceListener:CustomTraceListener
{
专用静态环形缓冲区_RingBuffer=新的环形缓冲区(200);
私有字符串tmp=string.Empty;
公共重写无效写入(字符串消息)
{
tmp+=消息;
}
公共覆盖无效写线(字符串消息)
{
_添加(tmp+消息);
tmp=String.Empty;
}
公共IEnumerable消息
{
得到
{
返回_ringBuffer.AsEnumerable();
}
}
}
}
它通常可以工作,但是我无法将其配置为使用自定义文本格式。在下面的配置中,我配置了两个跟踪侦听器,一个指向平面文件,另一个指向我的自定义侦听器。前者使用“简短格式化程序”正确记录日志,而后者失败并使用默认格式。为什么?
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
type="Sample.CircularTraceListener, CircularTraceListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
name="CircularTraceListener" formatter="Brief Formatter" />
<add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="trace.log" formatter="Brief Formatter" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="{timestamp} {message} {title} {dictionary({key} - {value}{newline})}"
name="Brief Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="CircularTraceListener" />
<add name="Flat File Trace Listener" />
</listeners>
</add>
</categorySources>
</loggingConfiguration>
您没有免费获得格式化程序-您需要编写跟踪侦听器,以便它获得格式化程序的实例并实际调用它。您没有免费获得格式化程序-您需要编写跟踪侦听器,以便它获得格式化程序的实例并实际调用它。Chris Tavares是正确的。解决办法如下:
namespace Sample1
{
using System;
using System.Collections.Generic;
using System.Linq;
using Cognoware.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class CircularTraceListener: CustomTraceListener
{
private string tmp = String.Empty;
public override void Write(string message)
{
tmp += message;
}
public override void WriteLine(string message)
{
tmp += message;
}
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
{
if ((this.Filter == null) || this.Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null))
{
if (data is LogEntry)
{
if (this.Formatter != null)
{
WriteLine(this.Formatter.Format(data as LogEntry));
}
else
{
base.TraceData(eventCache, source, eventType, id, data);
}
}
else
{
base.TraceData(eventCache, source, eventType, id, data);
}
}
}
}
}
克里斯·塔瓦雷斯是对的。解决办法如下:
namespace Sample1
{
using System;
using System.Collections.Generic;
using System.Linq;
using Cognoware.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class CircularTraceListener: CustomTraceListener
{
private string tmp = String.Empty;
public override void Write(string message)
{
tmp += message;
}
public override void WriteLine(string message)
{
tmp += message;
}
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
{
if ((this.Filter == null) || this.Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null))
{
if (data is LogEntry)
{
if (this.Formatter != null)
{
WriteLine(this.Formatter.Format(data as LogEntry));
}
else
{
base.TraceData(eventCache, source, eventType, id, data);
}
}
else
{
base.TraceData(eventCache, source, eventType, id, data);
}
}
}
}
}