We are using the RollingFileAppender in a web-applicfation running behind a load-balaner on multiple nodes. We noticed that logs of previous days often are very small, only a few lines, while the current log is large. It is not 100% consistent, about 1 in 5 previous logs apear to be a full log (using 2 nodes).
We figured that both nodes must be renaming log.log to the previous date with a minimal timespan between them. The last node to do so will actually overwrite the previous log with the new logfile created moments earlier by the first node.
This is our curated config:
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<file value="\\shared-path\log.log" />
<datePattern value="yyyy-MM-dd.'txt'"/>
<staticLogFileName value="true"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="20MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %message%newline"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
Is there any simple way to prevent this from hapening?
The problem can be solved by setting <staticLogFileName value="false"/>
.
In that case it will not rename log.log to the formatted archive name, but simply create a new file for the new log.
A disadvantage of this solution is that any Tail or monitoring tool will now have to check the directory for the newest file instead of just going for the static log.log.
Edit: This is not a viable solution. We are missing chunks of logs, the time sometimes goes back 1 minute so log4net seems to be caching before flushing to the file...
See also this answer to the question Logging web services operations behind load balancer