Writing to a new log file every day using TraceSource

I use a recorder in my application to write to files. The source, switch, and listeners were 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 do I need to do to create a new log file every day, and not write to the same log file every time?

+4
source share
2 answers

What do I need to do to create a new log file every day, and not write to the same log file every time?

You will need to make your TraceListener instead of TextWriterTraceListener . This will allow your TraceListener implementation to change the log files daily or to execute any other custom behavior.

+4
source
 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> 
-1
source

Source: https://habr.com/ru/post/1415993/


All Articles