Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4net generating wrong log file name

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?

like image 641
Vinicius Ottoni Avatar asked Aug 23 '12 11:08

Vinicius Ottoni


1 Answers

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>

like image 173
Thierry Avatar answered Sep 19 '22 13:09

Thierry