问题描述
我们看到开发和生产机器上出现间歇性问题,因此我们的日志文件没有被登录.
We are seeing an intermittent issue on development and production machines whereby our log files are not getting logged to.
使用Visual Studio在开发和调试中运行时,我们在VS输出窗口中收到以下log4net错误消息:
When running in development and debugging using Visual Studio we get the following log4net error messages in the VS output window:
log4net:ERROR [RollingFileAppender] Unable to acquire lock on file C:\folder\file.log.
该进程无法访问文件'C:\ folder \ file.log',因为它正在被另一个进程使用.
The process cannot access the file 'C:\folder\file.log' because it is being used by another process.
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" />
我们当前针对此问题的解决方法是重命名最后一个日志文件.我们当然希望它会失败(由于上述文件锁定),但通常不会.由于 aspnet_wp.exe 进程的锁定,重命名失败了一两次.
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配置部分如下所示:
Our log4net configuration section is shown below:
<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>
如前所述,我们在计算机上间歇性地看到了这种情况,但是一旦问题发生,问题仍然存在.
As mentioned, we are seeing this intermittently on machines, but once the issue happens it persists.
推荐答案
尝试添加
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
到<appender />
元素.之所以会影响性能,是因为这意味着log4net将锁定文件,对其进行写入并在每次写入操作时对其进行解锁(这与默认行为相反,后者会长时间获取并保持锁定状态).
to your <appender />
element. There is some performance impact because this means that log4net will lock the file, write to it, and unlock it for each write operation (as opposed to the default behavior, which acquires and holds onto the lock for a long time).
默认行为的一个含义是,如果在正在同一台计算机上运行的多个工作进程下执行的网站下使用它,则每个都会尝试无限期地获取并持有该锁,而另外两个他们中的只是输了.将锁定模型更改为最小锁定可以解决此问题.
One implication of the default behavior is that if you're using it under a Web site that is being executed under multiple worker processes running on the same machine, each one will try to acquire and hold onto that lock indefinitely, and two of them are just going to lose. Changing the locking model to the minimal lock works around this issue.
(调试时,不正常的终止和启动许多新的辅助进程正是可能发生的事情.)
(When debugging, ungraceful terminations and spinning up lots of new worker processes is exactly the type of thing that's likely to happen.)
祝你好运!
这篇关于间歇性的log4net RollingFileAppender锁定文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!