YWah
YWah

Reputation: 579

Log4net - Remove older log files based on date

Currently I'm using log4net version 1.2.10.0 to write logs for my .net window application. Is it true that if the rolling style is based on date, the older log files will not be removed automatically even if I set the statement below:

RollingFileAppender rollingFileAppender = new RollingFileAppender();
rollingFileAppender.MaxSizeRollBackups = 2;

If it is true, how can I removed those older log files automatically by writing some statements in my coding?

Upvotes: 0

Views: 2879

Answers (1)

bas
bas

Reputation: 14912

An example fragment of how to setup an appender:

  <appender name="ContextLogAppender" type="log4net.Appender.RollingFileAppender">
    <file value="..\Logs\ContextLog\context.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="100" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true"/>
    <countDirection value="1"/>
    <PreserveLogFileNameExtension value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss,fff} [%-5level][thread: %thread][%logger] %message%newline" />
    </layout>
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="PRODUCTION" />
    </filter>
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="CONTEXT" />
    </filter>
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="INFO" />
    </filter>
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="WARN" />
    </filter>
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="ERROR" />
    </filter>
    <filter type="log4net.Filter.LevelMatchFilter">
      <levelToMatch value="FATAL" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
  </appender>

This appender logs a bunch of categories to the log file. The attributes you are interested most in (I think)

    <appendToFile value="true" />

append to the last log file if true

    <rollingStyle value="Size" />

rolling style can be either size, date, or composite. I think you are looking for composite. In this example it's set to Size, meaning the rolling definition only looks at the amount of all produced log files. If the max amount of files is reached, then the oldest log file will be overwritten.

    <maxSizeRollBackups value="100" />

this is the definition of the max allowed log files for this appender.

    <maximumFileSize value="10MB" />

each log file created by this appender has a max limit size limit defined; 10MB per file. (so in this case: 100 * 10 MB = 1GB of log files is the highest amount of logging we allow; for this appender).

    <staticLogFileName value="true"/>

This makes sure I always write to the same log file, so that I can trust that when I look at context.log I look at the most recent logging. This will not work if you want to rely on date rolling.

Quote: The file size is different for each day, what I want to achieve is one file will be deleted for each day, so that the same amount of log files will be stored over the days

You aren't looking for a "rolling" window of loggings. You are looking for "backing up loggings" for each day. I don't think it's easy to achieve just by configuring log4net. It is possible to set the rolling style to Composite, so that it both looks for a date time pattern and a max amount of files.

E.g. you could configure log4net to write 10 log files a day with a max size for each produced log file.

<rollingStyle value="Composite" />
<datePattern value=".yyyyMMdd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />

This would result in max 100MB loggings per day. But that still doesn't answer your question...

HTH a bit though...

Upvotes: 1

Related Questions