Log4Net not rolling on MaxFileSize

Leo picture Leo · Dec 8, 2010 · Viewed 8.8k times · Source

Somehow my log4net not rolling to a new file when the old log file exceeds the MaxFileSize and also stop writing log information to the already exceeded one. But when I restart the server, it did rename the old one to ServerLog.txt.1 and create a new file ServerLog.txt and write to that file.

Here is my log4net.xml file:

<?xml version="1.0" encoding="utf-8"?>
<log4net debug="true">
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Logs\ServerLog.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="5MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    </layout>
  </appender>

  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
</log4net>

Can someone please help me? Thanks.

Answer

Steve Wranovsky picture Steve Wranovsky · Dec 16, 2010

I suspect your problem may arise from using the log from IIS. Since there is the possibility of having different app domains accessing the log file from within IIS, there may be issues with an app domain not being able to roll the file because the log file is also opened in another app domain.

You would need a locking model setup with your appender to prevent the app domains from colliding when accessing the file.

You should be able to do something like this:

<?xml version="1.0" encoding="utf-8"?>
<log4net debug="true">
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Logs\ServerLog.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="5MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    </layout>
    <lockingModel type="log4net.Appender.FileAppender+MutexLock" />
  </appender>

  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
</log4net>