我在log4net.config中有log4net的这个设置,允许多个线程写入同一个文件:
<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>
但午夜之后,新建的日志文件始终被覆盖,因此文件中只有最后一个事件.服务器重新启动后,它一直进行到下一个午夜.
所以也可以说这是一个配置问题,还是这只是一个log4net问题?
解决方法
由于我只有一个进程(IIS,w3wp.exe)使用相同的记录器,因此通过删除锁定模型键来解决问题.
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
既然是说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.
我想你会得到不可预测的结果.