log4net + multiple threads + rolling file appender
Asked Answered
M

2

12

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?

Mei answered 18/4, 2012 at 22:22 Comment(1)
any solution to let every thread write on different files (i.e. each thread has it's own file)?Gunderson
M
10

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.

Mei answered 28/5, 2012 at 9:31 Comment(0)
C
0

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.

Clatter answered 19/4, 2012 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.