最佳答案
我们在开发和生产机器上看到一个间歇性的问题,我们的日志文件没有被记录。
在使用 VisualStudio 进行开发和调试时,我们会在 VS 输出窗口中得到以下 log4net 错误消息:
log4net:ERROR [RollingFileAppender] Unable to acquire lock on file C:\folder\file.log.
该进程无法访问文件“ C: file file.log”,因为它正在被另一个进程使用。
log4net:ERROR XmlConfigurator: Failed to find configuration section 'log4net' in the application's .config file.
Check your .config file for the <log4net> and <configSections> elements.
配置部分应该是这样的:
<section
name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
Our current workaround for the issue is to rename the last log file. We would of course expect this to fail (due to the aforementioned file lock), but it normally doesn't. Once or twice the rename has failed due to a lock from the aspnet_wp.exe process.
Log4net 配置部分如下所示:
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\folder\file.log"/>
<appendToFile value="true" />
<datePattern value="yyyyMMdd" />
<rollingStyle value="Date" />
<maximumFileSize value="10MB" />
<maxSizeRollBackups value="100" />
<layout type="log4net.Layout.PatternLayout">
<header value="[Header]
"/>
<footer value="[Footer]
"/>
<conversionPattern value="%date %-5level %logger ${COMPUTERNAME} %property{UserHostAddress} [%property{SessionID}] - %message%newline"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
正如前面提到的,我们在机器上看到这种情况时有发生,但是一旦发生这种情况就会持续下去。