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?

有帮助吗?

解决方案

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.

其他提示

My guess is that your use of a - sign on the datePattern is confusing the framework so that after the first roll any log triggers a roll event.

What happens when you try this with

<datePattern value="yyyyMMdd" />

per the example here.

To change the rolling period adjust the DatePattern value. For example, a date pattern of "yyyyMMdd" will roll every day. See System.Globalization.DateTimeFormatInfo for a list of available patterns.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top