Log4Net 未在 MaxFileSize 上滚动

发布于 2024-10-06 07:18:12 字数 1002 浏览 7 评论 0原文

不知何故,当旧日志文件超过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-10-13 07:18:12

我怀疑您的问题可能是由于使用 IIS 的日志引起的。由于不同的应用程序域可能从 IIS 内访问日志文件,因此可能会出现应用程序域无法滚动文件的问题,因为日志文件也在另一个应用程序域中打开。

您需要使用附加程序设置锁定模型,以防止应用程序域在访问文件时发生冲突。

你应该能够做这样的事情:

<?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>
    <lockingModel type="log4net.Appender.FileAppender+MutexLock" />
  </appender>

  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
</log4net>

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:

<?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>
    <lockingModel type="log4net.Appender.FileAppender+MutexLock" />
  </appender>

  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
</log4net>
携余温的黄昏 2024-10-13 07:18:12

在配置中将“rollingStyle”更改为“Composite”对我有用。

<log4net debug="true">
  ....  
  <rollingStyle value="Composite"/>
  ....
</log4net>

Change "rollingStyle" to "Composite" in Config worked for me.

<log4net debug="true">
  ....  
  <rollingStyle value="Composite"/>
  ....
</log4net>
那一片橙海, 2024-10-13 07:18:12

我会尝试删除“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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文