Can Log4Net Delete Log Files Automatically?

34,077

Solution 1

Here is another question from here on SO that asks essentially the same thing: Log4Net: set Max backup files on RollingFileAppender with rolling Date

The consensus was that log4net does not support it directly. One poster says that it has been implemented in log4net's source code repository, but has not been released yet. Another poster shows some code that he uses to cleanup old files periodically.

Solution 2

I'm pretty sure that you can't do with the existing appender, although I can't confirm it.

However, I see two options:

  1. Create your own appender, subclassing RollingFileAppender (or, if you want more control, subclass FileAppender). Then change your config file to use that appender (change the appender element).
  2. Create a .bat file that deletes files older than x days (see: Batch file to delete files older than N days). Then create a task in Windows (http://support.microsoft.com/kb/308569) that runs this bat file e.g. every day.
Share:
34,077
tonyjy
Author by

tonyjy

Updated on July 10, 2022

Comments

  • tonyjy
    tonyjy almost 2 years

    I am using log4net RollingFileAppender in a windows service program written in C#. The number and size of files in logs directory is growing too fast, need cleanup. The configuration is below:

    <appender name="Rolling Log" type="log4net.Appender.RollingFileAppender">
    <file value="..\logs\MyProgram.%cs{instanceName}.log" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <datePattern value=".yyyy-MM-dd.lo\g" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="150MB" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%-5level] %property{remoteUser} [%threadIdentity] %type{1}.%method - %message%newline%exception" />
    </layout>
    

    I only want to keep 30 days of logs. How can I configure log4net to delete the logs automatically? If not available through log4net, what solutions would I have?

    Thank you in advance.

  • steinar
    steinar over 13 years
    He's already using the RollingFileAppender, and there is no information in this documentation on if you can do this or not.
  • tonyjy
    tonyjy over 13 years
    I like the first approach better. The second approach seems requiring more on the deployment than my current process and it is not easy to track.
  • tonyjy
    tonyjy over 13 years
    This option looks attractive to me because it seems easier than overriding RollingFileAppender. Now I am planning to add a log maintenance job, triggered by scheduler such as Quartz.Net.
  • wageoghe
    wageoghe over 13 years
    I thought that @jeff had a pretty good solution in his answer: stackoverflow.com/questions/95286/…. Essentially checking at app startup to see if it was time to delete a file (or files) or not. If your application is not too long running (i.e. not longer than 30 days at a time), then his approach might be sufficient.
  • Auguste Van Nieuwenhuyzen
    Auguste Van Nieuwenhuyzen over 9 years
    There isn't anything to do it.