如何配置log4net以在翻转时附加日期时间?

时间:2016-03-21 14:31:54

标签: log4net

这是我的追随者:

  <appender name="ApplicationRollingFile" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="logs/application.log" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <preserveLogFileNameExtension value="true"/>
    <datePattern value=".yyyy-MM-dd-hhmmss" />
    <maxSizeRollBackups value="50" />
    <maximumFileSize value="500KB" />
    <staticLogFileName value="false" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %level %logger %C.%M --- %message --- %newlineTrace:%newline %stacktrace%newline" />
    </layout>
  </appender>

目前输出的是:

application.time-logging-started.1.log
application.time-logging-started.2.log
application.time-logging-started.3.log

我想要的是:

application.time-of-first-rollover.log
application.time-of-second-rollover.log
application.time-of-third-rollover.log

这可以吗?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:0)

您的文件元素似乎使用PatternString作为文件名,滚动文件appender已经使用了名称转换。无需覆盖默认工作。

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  <file value="logs\" />
  <datePattern value="yyyy-MM-dd-hhmmss'.log'" />
  <staticLogFileName value="false" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <maxSizeRollBackups value="50" />
  <maximumFileSize value="500KB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
</appender>