log4net + multiple threads + rolling file appender

drunkcamel picture drunkcamel · Apr 19, 2012 · Viewed 14.1k times · Source

I've got this settings for log4net in the log4net.config to allow multiple threads to write to the same file:

<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <!-- Minimal locking to allow multiple threads to write to the same file -->
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    <file value="log\UI.log"/>
    <appendToFile value="true"/>
    <rollingStyle value="Date"/>
    <maxSizeRollBackups value="30"/>
    <datePattern value="-yyyyMMdd"/>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level [%property{identity}] %logger{3} - %message%newline"/>
    </layout>
</appender>

But after midnight the new created log file is being overwritten all the time and thus there is only the last one event in the file. After server restart it all goes right again till the next midnight.
So can anyone say whether this is a config issue or this is just a log4net issue?

Answer

drunkcamel picture drunkcamel · May 28, 2012

The problem was solved by removing the locking model key since I have only one process (IIS, w3wp.exe) which uses the same logger.

<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>

Since it was said here:

If you use RollingFileAppender things become even worse as several process may try to start rolling the log file concurrently. RollingFileAppender completely ignores the locking model when rolling files, rolling files is simply not compatible with this scenario.

I think you will get unpredictable results.