Formatting trace output

前端 未结 7 971
南笙
南笙 2020-12-29 09:14

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 poss

相关标签:
7条回答
  • 2020-12-29 09:51

    Even though this is old and an answer has been accepted, I will throw in one more option. You can use the Ukadc.Diagnostics addon from codeplex. Among other things, it enables you to define custom formatting, similar to the formatting that you can define with log4net and NLog. It is a configuration-only dependency. That is, you configure the use of Ukadc.Diagnostics through the app.config file. There are no source dependencies (you continue to log via System.Diagnostics not through a special api). Having said that, there are some limitations that you should be aware of:

    1. The formatting options currently implemented in Ukadc.Diagnostics really only work correctly when logging with TraceSources. When logging with Trace.Write and Trace.WriteLine the TraceEventCache object is not filled in and that is where most of the formatting objects get their information.

    2. You must use a Ukadc.Diagnostics TraceListener (or a custom listener derived from the Ukadc.Diagnostics base TraceListener) to get the custom formatting to appear in your output. If you found a new super duper rolling file TraceListener, you will have to do some work to use it in conjunction with the Ukadc.Diagnostics formatting. This might be as difficult as reimplementing the listener in terms of the Ukadc.Diagnostics base TraceListener. Or it could be easier, if you could just create a new Ukadc.Diagnostics-based TraceListener that contains the super duper rolling TraceListener, formats the messages per Ukadc.Diagnostics, and then delegates to the contained listener's Write/WriteLine methods.

    0 讨论(0)
提交回复
热议问题