5

I want to create log files names with the following pattern:

SBRF_20120820.log
SBRF_20120821.log
SBRF_20120822.log
SBRF_20120823.log

In other words, create a new file for each day. So, I create the following configuration to do that:

<log4net>
    <appender name="FileAppender" type="log4net.Appender.RollingFileAppender, log4net">
        <file type="log4net.Util.PatternString" value="Logs/SBRF_%date{yyyyMMdd}.log" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %-5level - %message%newline" />
        </layout>
    </appender>
    <logger name="LogEmArquivo">
        <level value="INFO" />
        <appender-ref ref="FileAppender" />
    </logger>
</log4net>

When I run the program, today for example, the file SBRF_20120823.log will be created. But in the following days the log keeps to write in the SBRF_20120823.log file, and the files that are created are:

SBRF_20120823.log.2012-08-23
SBRF_20120823.log.2012-08-24
SBRF_20120823.log.2012-08-25
SBRF_20120823.log.2012-08-26

And if I run the program tomorrow, the files that will be created are:

SBRF_20120824.log.2012-08-24
SBRF_20120824.log.2012-08-25
SBRF_20120824.log.2012-08-26
SBRF_20120824.log.2012-08-27

Why?

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Vinicius Ottoni
  • 4,631
  • 9
  • 42
  • 64

3 Answers3

7

You do not put the date pattern in the <file> - that is the static part of the filename. You need to put it into the <datePattern>.

Also, if you are using log4net 1.2.11, you can use <preserveLogFileNameExtension> which puts the datePattern on the current file also.

I think this is what you want your configuration to look like:

<log4net>
  <appender name="FileAppender" type="log4net.Appender.RollingFileAppender, log4net">
      <file type="log4net.Util.PatternString" value="Logs/SBRF_.log"/>
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd" />
      <preserveLogFileNameExtension value="true"/>
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date %-5level - %message%newline" />
      </layout>
  </appender>
  <logger name="LogEmArquivo">
      <level value="INFO" />
      <appender-ref ref="FileAppender" />
  </logger>

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Thierry
  • 1,031
  • 7
  • 16
  • +1. I used to write my own RollingFileAppender, but now switch to use the latest log4net with these new settings. – Lex Li Aug 24 '12 at 02:30
0

Remove the <rollingStyle value="Date" />.

nothrow
  • 15,882
  • 9
  • 57
  • 104
-1

Don't think you change this behaviour with the RollingFileAppender, so you would have to create your own appender.

sgmoore
  • 15,694
  • 5
  • 43
  • 67