简体   繁体   中英

Rollover on Log4Net

I read an article which told that :

rollingStyle can be either Date, Size or Composite. the default setting Composite, uses a combination of Size and Date settings. Thus if you have the datePattern set to “.yyyy-MM-dd” and maxSizeRollBackups set to 10, themn it will maintain 10 log backups for each day. If you have the DatePattern set to “.yyyy-MM-dd HH:mm” and maxSizeRollbackups = 10 then it will maintain 10 logfile backups per minute

and it said :

staticLogFileName indicates whether you need to keep writing (log) to the same file all the time. You will need to set it to false when using Date as the rolling style and you have large number of backups.

so i do it in my App.config :

<appender name="FileAppender" type="log4net.Appender.FileAppender">

  <file value="E:\operativity.log" />
  <staticLogFileName value="False" />
  <appendToFile value="true" />
  <rollingStyle value="Date" />
  <datePattern value=".yyyy-MM-dd HH:mm" />
  <maxSizeRollBackups value="3" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{dd/MM/yyyy HH:mm:ss.fff} - %level - %message%newline" />
  </layout>
</appender>

as you see, i set rollingStyle to Date and datePattern to .yyyy-MM-dd HH:mm and set maxSizeRollbackups to 3 , and i set staticLogFileName to False , so it should take three logs in every minute on separate files, but it does not!! what is the problem?

NOTE: this the Link of article

EDIT: I use it in a windowsservice and it gives me just one log file!

在此处输入图片说明

The problem is that you have a : (colon) in the datePattern, its an invalid char for filenames.

you also have to use RollingFileAppender not FileAppender.

Change

<datePattern value=".yyyy-MM-dd HH:mm" />

To

<datePattern value=".yyyy-MM-dd-HHmm" />

You will get one file because first file doesnt contain the datetime. And it will fail creating the other files because it contains the : (colon)

This one is tested and it works:

  <log4net>
    <appender name="Debug" type="log4net.Appender.RollingFileAppender">
      <file value="E:\operativity3.log" />
      <appendToFile value="true" />
      <maxSizeRollBackups value="3" />
      <datePattern value=".yyyy-MM-dd-HHmm" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO" />
        <param name="LevelMax" value="FATAL" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] [%level] %logger - %message%newline" />
      </layout>      
    </appender>    
    <root>
      <level value="ALL" />
      <appender-ref ref="Debug" />
    </root>
  </log4net> 

And here is the result:

在此处输入图片说明

Perhaps you haven't specified maximumFileSize and it uses a default value which is greater than the size of your log. Setting maxSizeRollBackups doesn't make sense when rolling on Date only. It wouldn't know how to split the logs. Also, as you checked it doesn't roll on seconds.

I would advice to use the Composite rolling and adjust the size so you have moreless 3 log files per minute.

Otherwise you can try creating your own appender that would look somthing like below.

    public class MyRollingFileAppender : log4net.Appender.RollingFileAppender
    {
        override protected void AdjustFileBeforeAppend()
        {
            if (RollingStyle == RollingMode.Date)
            {
                // decide if should roll
                if (shouldRoll)
                {  
                    RollOverTime(true);
                }
            }
            else
            {
                base.AdjustFileBeforeAppend();
            }
        }
    }

The source code for the log4net.Appender.RollingFileAppender you can download from here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM