Search code examples
c#tracesystem.diagnostics

How to include user friendly timestamp in traces


I am trying to understand the difference between Trace.Write vs Trace.TraceInformation and which one should be used.

I tried to configure traceOutputOptions for timestamp/datetime. I just need an add timestamp with each message that I am writing. The datetime I am getting is a bit messay as it appends application name and less user friendly time stamp in next line like below.

ConsoleApplication1.exe Information: 0 : Hello  - Trace!  
DateTime=2011-01-31T14:26:11.1538509Z  
ConsoleApplication1.exe Error: 0 : Hello  - Trace!  
DateTime=2011-01-31T14:26:11.1538509Z  

All I need is something like

2011-01-31 11:32 Information: Hello - Trace!  
2011-01-31 11:33 Error: Hello - Trace!

Is there any easy way of setting it up in App.config doing it?


Solution

  • Have a look at the Ukadc.Diagnostics project at codeplex. It provides a nice System.Diagnostics based addon package that provides more powerful output formatting capability (similar to log4net and NLog) than can be achieved with the built in System.Diagnostics TraceListeners. You can even write your own formatting/token objects and have them included in the output formatting process.

    The library is easy to use and works quite well.