0

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?

YWah
  • 571
  • 13
  • 38
  • 1
    Possible duplicate of [Can Log4Net Delete Log Files Automatically?](http://stackoverflow.com/questions/4259795/can-log4net-delete-log-files-automatically) – PHeiberg Dec 05 '15 at 13:13
  • There is still [an open issue](https://issues.apache.org/jira/browse/LOG4NET-27) in Log4net on this. – PHeiberg Dec 05 '15 at 13:14
  • may I ask why you care about the date at all? you can specify the amount of files it need to roll over. doesn't that do the trick as well? Or do you have very specific requirements for log files? – bas Dec 05 '15 at 13:18
  • @bas, may I know how can I specify the amount of files it need to rollover? 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. – YWah Dec 05 '15 at 14:12
  • for ordinary logging I don't care how much space I log, I just want to have loggings. I set the max size per log fil e.g. to 10MB, and allow log4net to use max 100 files. log4net takes care of the rest. I can post an example for that configuration, but I am not sure if that answers your question. If you want to implement very specific requirements wrt loggings I wonder if log4net suits best for you. – bas Dec 05 '15 at 19:50
  • @bas, can u post the example for the configuration? – YWah Dec 06 '15 at 06:17
  • Possible duplicate of [Log4Net: set Max backup files on RollingFileAppender with rolling Date](http://stackoverflow.com/questions/95286/log4net-set-max-backup-files-on-rollingfileappender-with-rolling-date) – stuartd Dec 06 '15 at 13:27

1 Answers1

1

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...

bas
  • 13,550
  • 20
  • 69
  • 146