views:

298

answers:

6

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.

+3  A: 

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 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);
}
Jon Skeet
+1  A: 

You could write your own TextWriterTraceListener subclass which overrides the WriteLine methods, decorates the line, and then passes the decorated string to the base class implementation to do the actual output.

Steve Gilham
A: 

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.

Russell Troywest
A: 

Consider using The Logging Application Block

Dzmitry Huba
A: 

Or just add "DateTime" as a traceOutputOption.

As written on http://msdn.microsoft.com/en-us/library/system.diagnostics.tracelistener.traceoutputoptions.aspx this parameter is not used by `Write` and `WriteLine` methods of `TextWriterTraceListener` which makes it useless in my case
RaYell
A: 

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.

wageoghe