格式化跟踪输出 [英] Formatting trace output

查看:142
本文介绍了格式化跟踪输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 TextWriterTraceListener会登录诊断信息到一个文本文件中。不过,我也wan't登录加入每一丝消息的时间戳。是否有可能定义一种格式化为侦听器将自动添加时间戳?

I'm using TextWriterTraceListener to log diagnostics messages to a text file. However I wan't also to log a timestamp of every trace message added. Is it possible to define a kind of formatter for the listener that would automatically add timestamps?

目前,我在每一个跟踪手动添加时间戳。的WriteLine()呼叫,但这个不是很舒服。

Currently I'm adding timestamps manually on every Trace.WriteLine() call but this isn't very comfortable.

推荐答案

我建议你使用<一个HREF =htt​​p://logging.apache.org/log4net/index.html> log4net的相反,它具有更多的可定制性。

I suggest you use Log4Net instead, which has a lot more customizability.

另外你可以写它穿上了时间戳你自己的的TraceListener 的实施。您的可以的甚至能刚刚从<$ ​​C $ C> TextWriterTraceListener会派生并重写的WriteLine

Alternatively you could write your own TraceListener implementation which put the timestamps on for you. You may even be able just derive from TextWriterTraceListener and override Write and WriteLine:

public override void Write(string x)
{
     // Use whatever format you want here...
     base.Write(string.Format("{0:r}: {1}", DateTime.UtcNow, x));
}

public override void WriteLine(string x)
{
     // Use whatever format you want here...
     base.WriteLine(string.Format("{0:r}: {1}", DateTime.UtcNow, x));
}

这篇关于格式化跟踪输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆