Log4Net 未在 MaxFileSize 上滚动
不知何故,当旧日志文件超过 MaxFileSize 时,我的 log4net 不会滚动到新文件,并且也停止将日志信息写入已超出的文件。但是当我重新启动服务器时,它确实将旧服务器重命名为 ServerLog.txt.1 并创建一个新文件 ServerLog.txt 并写入该文件。
这是我的 log4net.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\ServerLog.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
有人可以帮助我吗?谢谢。
Somehow my log4net not rolling to a new file when the old log file exceeds the MaxFileSize and also stop writing log information to the already exceeded one. But when I restart the server, it did rename the old one to ServerLog.txt.1 and create a new file ServerLog.txt and write to that file.
Here is my log4net.xml file:
<?xml version="1.0" encoding="utf-8"?>
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\ServerLog.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
Can someone please help me? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我怀疑您的问题可能是由于使用 IIS 的日志引起的。由于不同的应用程序域可能从 IIS 内访问日志文件,因此可能会出现应用程序域无法滚动文件的问题,因为日志文件也在另一个应用程序域中打开。
您需要使用附加程序设置锁定模型,以防止应用程序域在访问文件时发生冲突。
你应该能够做这样的事情:
I suspect your problem may arise from using the log from IIS. Since there is the possibility of having different app domains accessing the log file from within IIS, there may be issues with an app domain not being able to roll the file because the log file is also opened in another app domain.
You would need a locking model setup with your appender to prevent the app domains from colliding when accessing the file.
You should be able to do something like this:
尝试将appendToFile 值设置为false。请参阅 http://logging.apache.org/log4net/发布/sdk/log4net.Appender.RollingFileAppender.RollingStyle.html
Try setting the appendToFile value to false. See http://logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFileAppender.RollingStyle.html
在配置中将“rollingStyle”更改为“Composite”对我有用。
Change "rollingStyle" to "Composite" in Config worked for me.
我会尝试删除“staticLogFileName”。我们使用与您几乎相同的配置设置;但我们不指定 staticLogFileName 属性。 (这是一个猜测;因为将 staticLogFileName 设置为 true 应该没问题)
I would try removing the 'staticLogFileName'. We use pretty much the same config settings as you; but we donot specify staticLogFileName property. (This is a shot-in-the-dark; because it should be okay to set the staticLogFileName to true)