32

In my C# .NET application I have an issue with the Trace.WriteLine()-method. I uses this method alot, and want to add a TimeStamp every time I use it.

Instead of Trace.WriteLine(DateTime.Now + " Something wrong!"), is there a solution where the DateTime is on default?

1

6 Answers 6

101

Via code

You can configure the TraceOutputOptions flags enum.

var listener = new ConsoleTraceListener() { TraceOutputOptions = TraceOptions.Timestamp | TraceOptions.Callstack }; Trace.Listeners.Add(listener); Trace.TraceInformation("hello world"); 

This does not work for Write and WriteLine, you have use the TraceXXX methods.

Via app.config

This can also be configured in your App.config with a somewhat equivalent and using TraceSource:

<configuration> <system.diagnostics> <trace autoflush="true"> <sources> <source name="TraceSourceApp"> <listeners> <add name="myListener" type="System.Diagnostics.ConsoleTraceListener" traceOutputOptions="Timestamp" /> </listeners> </source> </sources> </trace> </system.diagnostics> </configuration> 

And in code you can:

private static TraceSource mySource = new TraceSource("TraceSourceApp"); static void Main(string[] args) { mySource.TraceInformation("hello world"); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Config file equivalent: traceOutputOptions = "DateTime, Timestamp" to the system.diagnostics/sources/listeners/add tag. Save an MSDN lookup - http://msdn.microsoft.com/en-us/library/a10k7w6c(v=vs.110).aspx, and it is mentioned in one of the other answers here. Somehow ended up seeing the MSDN one before the other answer.
Note that the trace output options do not apply to Trace.WriteLine, only Trace.TraceInformation / Trace.Trace* methods.
20

You can set the app.config file to use a timestamp (relative and current time) for all trace listeners using the traceOutputOptions

traceOutputOptions = "DateTime, Timestamp"; 

2 Comments

As per one of the more popular answers, this is only effective when the TraceXXX methods are used.
This is the approach when using the trace listeners <listeners> <add name="fileTrace" traceOutputOptions = "DateTime, Timestamp" type="System.Diagnostics.TextWriterTraceListener" initializeData="trace.log" /> <remove name="Default" /> </listeners>
3

Just write your own "TraceLine(string msg)" method and start calling that:

void TraceLine(string msg, bool OmitDate) { if (!OmitDate) msg = DateTime.Now + " " + msg; Trace.WriteLine(msg); } void TraceLine(string msg) {TraceLine(msg, false);} 

3 Comments

These should be static methods, as no dependency on the object calling them.
I'd suggest logging with UTC timestamps to avoid any ambiguity in timezones etc. - also you might want to use Format or StringBuilder to avoid all those temporary strings
actually, the most efficient string concatenation method is probably string.Concat(). Not that it would matter anyway.
1

You could write a wrapper method that did it:

public static void DoTrace(string message) { DoTrace(message,true); } public static void DoTrace(string message, bool includeDate) { if (includeDate) { Trace.WriteLine(DateTime.Now + ": " + message); } else { Trace.WriteLine(message); } } 

2 Comments

This wouldn't work because DoTrace is static and you don't have access to Trace. You could use HttpContext.Current.Trace.WriteLine(DateTime.Now + ": " + message)
Isn't System.Diagnostics.Trace.WriteLine() also static? Depends which he's using I guess :/
0

We use log4net TraceAppender where you can config layout or filtering easily.

Comments

0

You could write your own TextWriterTraceListener, as mentioned here.

Comments