Cipher
Cipher

Reputation: 6082

Writing to a new log file each day with TraceSource

I am using a logger in my application to write to files. The source, switch and listeners have been defined in the app.config file as follows:

  <system.diagnostics>
    <sources>
      <source name="LoggerApp" switchName="sourceSwitch" switchType="System.Diagnostics.SourceSwitch">
        <listeners>
          <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="myListener.log" />
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="sourceSwitch" value="Information" />
    </switches>
  </system.diagnostics>

Inside, my .cs code, I use the logger as follows:

private static TraceSource logger = new TraceSource("LoggerApp");
logger.TraceEvent(TraceEventType.Information, 1, "{0} : Started the application", DateTime.Now);

What would I have to do to create a new log file each day instead of writing to the same log file every time?

Upvotes: 4

Views: 4207

Answers (2)

zrabzdn
zrabzdn

Reputation: 995

Try use:

<system.diagnostics>
    <sources>
      <source name="LoggerApp" switchName="sourceSwitch" switchType="System.Diagnostics.SourceSwitch">
        <listeners>
          <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="myListener-{0:dd}-{0:MM}-{0:yyyy}.log" />
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="sourceSwitch" value="Information" />
    </switches>
  </system.diagnostics>

Upvotes: -1

Reed Copsey
Reed Copsey

Reputation: 564333

What would I have to do to create a new log file each day instead of writing to the same log file every time?

You'd have to make your own TraceListener instead of using TextWriterTraceListener. This would allow your TraceListener implementation to change log files daily, or do any other custom behavior you wish.

Upvotes: 5

Related Questions