Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log files rotation with .net listeners

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

<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 becomes quickly very large, so I was wondering if it's possible to configure some sort of log rotation.
Since the process locks the file for writing, it's not possible to rotate it manually, say with a script periodically renaming the file, at least without stopping and restarting the service.

Thanks for any suggestion.

like image 914
Paolo Tedesco Avatar asked Jan 24 '23 14:01

Paolo Tedesco


1 Answers

There is the FileLogTraceListener that I think would do the trick. You can configure it to a daily or weekly interval.

If it's not enough then you will have to write your own tracelistener, just inherit from TraceListener and override the write methods.

like image 183
jmservera Avatar answered Jan 30 '23 04:01

jmservera