Rotating log files using .net listeners

I have an existing .net service and I want to configure it to write messages to a log file. In the service configuration, I added the following:

<system.diagnostics> <sources> <source name="My.Service" switchValue="All"> <listeners> <add name="text" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\log.txt" traceOutputOptions="Timestamp"/> </listeners> </source> </sources> </system.diagnostics> 

The problem is that the log file is becoming very fast, so I was wondering if it was possible to configure some kind of log rotation. Since the process locks the file for writing, it cannot be rotated manually, say, with a script, periodically renaming the file, at least without stopping and restarting the service.

Thanks for any suggestion.

+4
source share
3 answers

There is a FileLogTraceListener which I think will do the trick. You can set it to a daily or weekly interval.

If this is not enough, you will have to write your own tracelistener, just inherit from TraceListener and override the recording methods.

+8
source

You have a log rotation built into the file name to:

"C: \ log.txt"

becomes:

"C: \ logDDMMYYYY.txt"

Thus, the log automatically switches to a new file at the end of the day.

0
source

Perhaps you can take a look at log4net's free and easy-to-use logging library for .NET. This library already includes log rotation.

Otherwise, you will have your service to "unlock" the file and / or enter the new file automatically.

0
source

All Articles