Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net + multiple threads + rolling file appender

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?

like image 202
drunkcamel Avatar asked Apr 18 '12 22:04

drunkcamel


1 Answers

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.

like image 159
drunkcamel Avatar answered Sep 17 '22 12:09

drunkcamel