自定义 TraceListener 和多个消息
我在使用自定义 TraceListener 时遇到一些困难。问题是写入单个跟踪行会产生两次调用,一次调用 Write(),另一个调用 WriteLine()。对 Write() 的调用包含跟踪源、级别和事件 ID。对 WriteLine() 的调用是实际的消息。
看起来跟踪侦听器仅实例化一次,因此我不能只对 Write() 的第一次调用进行排队。看起来没有办法将这两个调用关联起来。不幸的是,这是一个问题,因为它导致我向远程服务发送 2 条消息,从而使开销加倍。
似乎也没有任何通用的方法来过滤呼叫。我会接受忽略带有源和级别的调用,但这似乎很容易发生。
下面是一段示例代码:
/// <summary>
/// When overridden in a derived class, writes the specified message to the listener you create in the derived class.
/// </summary>
/// <param name="message">A message to write. </param><filterpriority>2</filterpriority>
public override void Write(string message)
{
_client.Post(message);
}
/// <summary>
/// When overridden in a derived class, writes a message to the listener you create in the derived class, followed by a line terminator.
/// </summary>
/// <param name="message">A message to write. </param><filterpriority>2</filterpriority>
public override void WriteLine(string message)
{
_client.Post(message);
}
用法是:
private static readonly TraceSource Ts = new TraceSource("Source");
Ts.TraceEvent(TraceEventType.Error, 0, "Error Message");
将生成对 Write() 的调用:
“来源:错误:0”
然后调用 WriteLine()
“错误消息”
是否可以将这两个消息结合起来?还是只过滤第一个?谢谢!
I'm having some difficulity with a custom TraceListener. The issue is that writing a single trace line produces two calls, one to Write(), the other to WriteLine(). The call to Write() contains the trace source, level, and event id. The call to WriteLine() is the actual message
It looks like the trace listener is instantiated only once, so I can't just queue the first call to Write(). It looks like there's no way to correlate the two calls. Unfortunately this is a problem, since it causes me to send 2 messages to the remote service, doubling the overhead.
There doesn't seem any generic way to filter the calls either. I'd accept just ignoring the call with the source and level, but it seems like that might be very prone.
Here's a sample piece of code:
/// <summary>
/// When overridden in a derived class, writes the specified message to the listener you create in the derived class.
/// </summary>
/// <param name="message">A message to write. </param><filterpriority>2</filterpriority>
public override void Write(string message)
{
_client.Post(message);
}
/// <summary>
/// When overridden in a derived class, writes a message to the listener you create in the derived class, followed by a line terminator.
/// </summary>
/// <param name="message">A message to write. </param><filterpriority>2</filterpriority>
public override void WriteLine(string message)
{
_client.Post(message);
}
Usage is with:
private static readonly TraceSource Ts = new TraceSource("Source");
Ts.TraceEvent(TraceEventType.Error, 0, "Error Message");
Will produce a call to Write() with:
"Source: Error: 0"
And then a call to WriteLine() with
"Error Message"
Is it possible to combine the two messages? Or just filter the first? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有 2 条消息,有 2 种不同的格式……这些消息要写到哪里?这是使用企业日志记录块吗?如果是这样,您应该检查配置文件 - 侦听器可能会注册两次。
There's 2 messages with 2 different formats... where are these writing to? Is this using the Enterprise Logging block? If so, you should check the config file - the listener might be registered twice.
我能够通过实现来自 Ukadc Diagnostics 的 TraceListener 基类来解决这个问题。
基类是:
和我的自定义 TraceListener :
您可以在我的 github 帐户 上找到一个工作示例
I was able to solve this by implementing the TraceListener base class from Ukadc Diagnostics
The base class is:
and my custom TraceListener:
You can find a working example on my github account