解决Log4net多进程访问日志文件

<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<!--日志路径-->
<param name="File" value="log/err/" />
<param name="Encoding" value="utf-8" />
<!--是否是向文件中追加日志-->
<param name="AppendToFile" value="true" />
<!--log保留天数-->
<param name="MaxSizeRollBackups" value="10" />
<!--日志文件名是否是固定不变的-->
<param name="StaticLogFileName" value="false" />
<!--日志文件名格式为:2008-08-31.log-->
<param name="DatePattern" value="yyyy-MM-dd&quot;error.log&quot;" />
<!--日志根据日期滚动-->
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<!-- <param name="Header" value="[Header] " /> -->
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
<!-- <param name="Footer" value="%n [Footer] " /> -->
<!-- <conversionPattern -->
<!-- value="&lt;HR COLOR=red>%n时间:%d [%t] &lt;BR>%n级别:%-5p &lt;BR>%n类:%c [%x] &lt;BR>%n%m &lt;BR>%n &lt;HR Size=1>" /> -->

</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMax" value="Error" />
<param name="LevelMin" value="Error" />
</filter>
</appender>

<root>
<!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
<level value="all" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
</configuration>

 

多进程读取文件,会造成日志文件被占用,如下:
正由另一个进程使用,因此该进程无法访问此文件

需要在Log4net.config添加该配置项

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

 

上一篇:C# log4net 的配置


下一篇:Log4net配置检查点