Appending date to filename when rolling by size

有些话、适合烂在心里 提交于 2019-12-12 15:19:09

问题


I'd like to configure log4net to append the name to the filenames when rolling by size. I've only played around with the configuration section of log4net, not sure if I have to configure something in code for this.
This is what I have now:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="C:\\rolling-log.txt" />
  <appendToFile value="true" />
  <datePattern value="_yyyy-MM-dd" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10KB" />
  <rollingStyle value="Size" />
  <staticLogFileName value="true" />
  <preserveLogFileNameExtension  value="true"/>
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%m%n" />
  </layout>
</appender>

I can only get the date in the filename if the rollingStyle is set to Date, but then it doesn't roll on size anymore.

What am I missing here?


回答1:


Change

<file value="C:\\rolling-log.txt" />

to

<file type="log4net.Util.PatternString" value="C:\\rolling-log-%date{yyy-MM-dd}.txt" />

(or whatever date pattern you prefer).

Caveat:

Unfortunately, the date specified in the filename is only evaluated upon configuration, meaning the same date will be used on each roll until log4net is reconfigured. I have not found a way to fix this.



来源:https://stackoverflow.com/questions/16858935/appending-date-to-filename-when-rolling-by-size

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