Log4net - delete any logs that is over 14 days old

DanCode picture DanCode · Jul 17, 2015 · Viewed 14.7k times · Source

I am creating a logger by using log4net's RollingFileAppender. One feature that I need to provide is to delete any logs that is over 14 days old.

How is that possible with using log4net's RollingFileAppender?

Thanks.

Answer

BhavO picture BhavO · Jul 17, 2015

Try this

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
  <file value=".\Logs\" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <staticLogFileName value="false" />
  <rollingStyle value="Date" />
  <datePattern value="'On_'yyyy-MM-dd'.log'" />
  <appendToFile value="true" />
  <maxSizeRollBackups value="14" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
  </layout>
</appender>