Log4net RollingFileAppender is Overwriting file and not appending number to end

一曲冷凌霜 提交于 2020-04-18 04:17:41

问题


I am using log4net in an application with a RollingFileAppender. I have the rollingStyle set to "Composite" and staticLogFileName to "false" but when the maximumFileSize is reached it overwrites the current file rather than appending a 1 to the end. Below is my config code:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <root>
    <level value="INFO" />
    <appender-ref ref="console" />
    <appender-ref ref="RollingFileAppender"/>
  </root>
  <appender name="console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="logs\" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    <datePattern value="yyyyMMdd'.log'" />
    <staticLogFileName value="false" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <maximumFileSize value="10KB" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level %logger - %message%newline" />
    </layout>
  </appender>
</log4net>

The date part works correctly when the day rolls over but i cant work out why the file size does not. Please note the 10kb size is only to test the functionality and in production will be a greater size.

Can anyone help?

Thanks


回答1:


You haven’t configured maxSizeRollBackups, by default it is 0, so there will be no backup files and the log file will be truncated when it reaches maximumFileSize.

Configure as

<maxSizeRollBackups value="10" />

Note that a value of 10 in combination with the yyyyMMdd DatePattern will keep 10 files per day.



来源:https://stackoverflow.com/questions/54579061/log4net-rollingfileappender-is-overwriting-file-and-not-appending-number-to-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!