TextWriterTraceListener 在每行开头写入 app filename.exe
我正在使用自己的自定义 TextWriterTraceListener 向每个记录的行添加时间戳,以便输出如下所示:
LoggingExperiments.exe Information: 0 : 13:11 Testing infos
LoggingExperiments.exe Error: 0 : 13:11 Testing errors
这是代码:
class CustomTextWriterTraceListener : TextWriterTraceListener
{
public CustomTextWriterTraceListener(string file) : base(file)
{
}
public override void WriteLine(string message)
{
base.Write(DateTime.Now.ToShortTimeString());
base.Write("\t");
base.WriteLine(message); // #1
Writer.WriteLine(message); // #2
}
}
如何格式化输出,使其在每行上都没有前导可执行文件名称?或者更改记录信息的顺序?
我应该始终调用“Writer.WriteLine”并格式化它的参数(注释#2),还是有其他方法可以覆盖此行为并继续调用基本方法,如标记为#1的代码中所示。
I'm using my own custom TextWriterTraceListener for adding timestamp to each logged line so that the output looks like this:
LoggingExperiments.exe Information: 0 : 13:11 Testing infos
LoggingExperiments.exe Error: 0 : 13:11 Testing errors
Here is the code:
class CustomTextWriterTraceListener : TextWriterTraceListener
{
public CustomTextWriterTraceListener(string file) : base(file)
{
}
public override void WriteLine(string message)
{
base.Write(DateTime.Now.ToShortTimeString());
base.Write("\t");
base.WriteLine(message); // #1
Writer.WriteLine(message); // #2
}
}
How to format the output so that it doesn't have leading executable names on each line? Or to change the order of information logged?
Should I always call "Writer.WriteLine" and format it's parameters (comment #2) or there is some other way to override this behavior and stay with calling base methods like in code marked as #1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法重现你的行为...
我已经尝试过这段代码:
输出是:
您可以在配置文件中执行以下操作:
看看 traceOutputOptions
此处 MSDN 参考
Can't reproduce your behavior...
I've tried this code :
And Output is :
You can do in configuration file :
Take a look at traceOutputOptions
Here the MSDN reference