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?
Currently I'm adding timestamps manually on every Trace.WriteLine()
call but this isn't very comfortable.
I recently encountered similar situation and it looks like now we have a tool very much fit for the task, namely Essential Diagnostics. You set up a listener in app.config like in code below and then just place
Essential.Diagnostics.dll
into the same folder. NO RECOMPILING IS REQUIRED. You can use this with any applications that uses System.Diagnostics for tracing, even if you do not own the source. Isn't that marvelous?Not really an answer to your question but have you considered just using log4Net?
You can configure it to add times etc, along with a vast amount of other useful functionality.
I suggest you use Log4Net instead, which has a lot more customizability.
Alternatively you could write your own
TraceListener
implementation which put the timestamps on for you. You may even be able just derive fromTextWriterTraceListener
and overrideWrite
andWriteLine
:As noted in comments, this ends up with date duplication for
TraceInformation
, because that callsWrite
twice. Using a "proper" logging framework is definitely better.Or just add "DateTime" as a traceOutputOption.
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:
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.
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.
Consider using The Logging Application Block