log4net RollingFileAppender spewing tons of logs, maxSizeRollBackups has no effect
Asked Answered
A

2

6

A service I have is suddenly spewing tons of log files and is not limiting the number of files written. The logs are (sort of) named MyService.2015-01-08.1, MyService.2015-01-08.2, MyService.2015-01-08.3, etc, all the way until 218 currently. I'd like to limit this to 10 per day instead of it being unlimited like right now. Here is what the log config was previously:

<appender name="RollingFileAppender" type="Ourlib.Logging.CustomRollingFileAppender">
  <file value="c:\logs\myservice" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <staticLogFileName value="false" />
  <maxSizeRollBackups value="-1" />
  <countDirection value="1" />
  <maximumFileSize value="5000KB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%utcdate [%thread] %-5level %property{CorrelationId} %property{CallPath} %logger{2} - %message%newline" />
  </layout>
</appender>

I've made some changes, seen below. Notably, I've set maxSizeRollBackups to 10, which I thought would fix the problem, but doesn't seem to have made a difference.

<appender name="RollingFileAppender" type="Ourlib.Logging.CustomRollingFileAppender">
  <file value="c:\logs\myservice" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <staticLogFileName value="false" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="5000KB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%utcdate [%thread] %-5level %property{CorrelationId} %property{CallPath} %logger{2} - %message%newline" />
   </layout>
</appender>

The change has been deployed but it's still creating new log files, way past the newly set 10.

Assuming that my deploy was done correctly, what could be wrong? Am I misunderstanding the config properties I changed, or is there something else going on here?

Thanks all!

Alkalify answered 9/1, 2015 at 3:13 Comment(3)
Did you mean to drop <countDirection value="1" />?Lanford
Yes, I should've mentioned that as well.Alkalify
You're using rollingStyle.Composite so the file will roll on size AND date, but you're not specifying a DatePattern - at least not in config, is it set in your custom appender? This rolling by date is why the number of backup files exceeds the maxSizeRollBackups setting sa that only applies to files rolled by size, not by date, as stated in the documentation: "A maximum number of backup files when rolling on date/time boundaries is not supported."Luxemburg
K
5

Try adding DatePattern and keep your change of maxSizeRollBackups value as 10 like this

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
     <file value="c:\logs\myservice" />
     <appendToFile value="true" />
     <rollingStyle value="Composite" />
     <datePattern value=".yyyy-MM-dd" />
     <maxSizeRollBackups value="10" />
     <maximumFileSize value="5000KB" />
     <countDirection value="1"/>
     <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
     </layout>
</appender>

This is a Composite RollingFileAppender which keeps max of 10 5000KB log backups per day

Kinsella answered 10/1, 2015 at 16:15 Comment(0)
B
-1

Try this instead:

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
  <file value=".\App.log" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <appendToFile value="true" />
  <maximumFileSize value="10MB" />
  <maxSizeRollBackups value="5" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
  </layout>
</appender>

Another example:

<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" />
  <maximumFileSize value="10MB" />
  <maxSizeRollBackups value="10" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
  </layout>
</appender>

If you have to use your custom one, then I guess what is missing is:

  1. A whack at the end of file value attribute (<file value="c:\logs\myservice\" />).
  2. Composite file definition, unless your custom appender does it already.
  3. Use lockingModel if you don't have BufferedAppender in front of this one.
Blanco answered 9/1, 2015 at 3:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.