简体   繁体   English

在Log4Net上滚动

[英]Rollover on Log4Net

I read an article which told that : 我读了一篇文章,内容是:

rollingStyle can be either Date, Size or Composite. RollingStyle可以是Date,Size或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. 因此,如果将datePattern设置为“ .yyyy-MM-dd”,并将maxSizeRollBackups设置为10,则它将每天维护10次日志备份。 If you have the DatePattern set to “.yyyy-MM-dd HH:mm” and maxSizeRollbackups = 10 then it will maintain 10 logfile backups per minute 如果您将DatePattern设置为“ .yyyy-MM-dd HH:mm”并且maxSizeRollbackups = 10,则它将每分钟维护10个日志文件备份

and it said : 它说:

staticLogFileName indicates whether you need to keep writing (log) to the same file all the time. staticLogFileName指示是否需要一直保持向同一文件写入(日志)。 You will need to set it to false when using Date as the rolling style and you have large number of backups. 当使用“日期”作为滚动样式并且您有大量备份时,需要将其设置为false。

so i do it in my App.config : 所以我在我的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!! 如您所见,我将rollingStyle设置为Date并将datePattern.yyyy-MM-dd HH:mm ,并将maxSizeRollbackups设置为3 ,并且将staticLogFileName设置为False ,所以它应该每分钟在单独的文件上花费三个日志,但是它确实不!! 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! 编辑:我在windowsservice使用它,它只给我一个日志文件!

在此处输入图片说明

The problem is that you have a : (colon) in the datePattern, its an invalid char for filenames. 问题在于datePattern中有一个:(冒号),它是文件名的无效字符。

you also have to use RollingFileAppender not FileAppender. 您还必须使用RollingFileAppender而不是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. 也许您没有指定maximumFileSize ,它使用的默认值大于日志的大小。 Setting maxSizeRollBackups doesn't make sense when rolling on Date only. 仅在Date上滚动时,设置maxSizeRollBackups没有意义。 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. 我建议您使用复合滚动并调整大小,以便每分钟有3个以上的日志文件。

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 . 您可以从此处下载log4net.Appender.RollingFileAppender的源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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