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?

  • You can use [Essential Diagnostics](http://essentialdiagnostics.codeplex.com/) just [like so](http://stackoverflow.com/a/11361706/1353187) – Eugene Ryabtsev Jul 06 '12 at 11:56

6 Answers6

102

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");
}
morhook
  • 685
  • 7
  • 19
user38309
  • 2,268
  • 3
  • 21
  • 33
  • 6
    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](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. – Mayyit Nov 26 '13 at 14:43
  • 8
    Note that the trace output options do not apply to Trace.WriteLine, only Trace.TraceInformation / Trace.Trace* methods. – Curtis Yallop Nov 12 '14 at 18:12
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";
dandan78
  • 13,328
  • 13
  • 64
  • 78
johara56
  • 210
  • 2
  • 4
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);}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 5
    These should be static methods, as no dependency on the object calling them. – JDunkerley May 14 '09 at 13:51
  • 1
    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 – morechilli May 14 '09 at 13:52
  • actually, the most efficient string concatenation method is probably string.Concat(). Not that it would matter anyway. – DonkeyMaster May 14 '09 at 15:15
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);
    }
}
Lloyd
  • 29,197
  • 4
  • 84
  • 98
  • 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) – Darin Dimitrov May 14 '09 at 13:47
  • 2
    Isn't System.Diagnostics.Trace.WriteLine() also static? Depends which he's using I guess :/ – Lloyd May 14 '09 at 13:59
0

You could write your own TextWriterTraceListener, as mentioned here.

Community
  • 1
  • 1
tm1
  • 1,180
  • 12
  • 28
0

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

treehouse
  • 2,521
  • 1
  • 21
  • 39