Search code examples
c#timestamptracelistener

Can I put a timestamp in initializeData using TraceListener?


It is possible to put a timestamp in the "initializeData" on the TraceListener?

Something like this:

<sharedListeners>
      <add name="AppSourceListener"
        type="System.Diagnostics.DelimitedListTraceListener" 
        initializeData="c:\test%date.csv" 
        traceOutputOptions="ProcessId, DateTime, Callstack">
      </add>      
</sharedListeners>

I would like to put the DateTime.Now in every log, once the application is initiated.


Solution

  • This is not possible from .config file. To do that create TraceListener from code:

    //Remove all existing trace listeners
    while (Trace.Listeners.Count > 0)
        Trace.Listeners.RemoveAt(0);
    //Add the new one with new file name
    Trace.Listeners.Add(new DelimitedListTraceListener(@"mylogwithdatetime.log"));