为什么在使用RollingFileAppender时log4net创建两个单独的日志文件? [英] Why is log4net creating two separate log files when using RollingFileAppender?

查看:285
本文介绍了为什么在使用RollingFileAppender时log4net创建两个单独的日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使log4net RollingFileAdapter工作,以便我的日志文件按日期滚动.但是我发现,即使我复制示例代码,我没有得到我期望的行为.它没有获取当前日期和时间的单个文件,而是将日志消息拆分为两个不同的文件.一个文件称为"log",第二个文件遵循配置,将被称为"log20130830-1115.txt".

I'm trying to get the log4net RollingFileAdapter working so that my logfiles are rolling over by date. However I'm finding that even when I copy the example code, I'm not getting the behaviour I expect. Instead of getting a single file of today's date and time, it splits the log messages between two different files. One file is called just "log" and the second one obeys the config and will be called 'log20130830-1115.txt'.

如果在配置文件中使用<log4net debug="true">,则会在跟踪输出中看到以下提示:

If I use <log4net debug="true"> in my config file, I see the folling in the Trace output:

log4net: Initial roll over to [c:\inetpub\wwwroot\QuartzTest\ScheduleTest\bin\Debug\log20130830-1115.txt]
log4net: Moving [c:\inetpub\wwwroot\QuartzTest\ScheduleTest\bin\Debug\log] -> [c:\inetpub\wwwroot\QuartzTest\ScheduleTest\bin\Debug\log20130830-1115.txt]

注意第二行...为什么首先创建一个名为"log"的文件?而且为什么它似乎没有按照它所说的那样做呢? "log"中的条目始终在正确命名的文件中的任何条目之后加上时间戳,即使该文件最先出现.

Notice line two... Why does it create a file called "log" in the first place? And why doesn't it seem to do the move it talks about? The entries in 'log' are always timestamped AFTER any of the entries in the correctly named file, even though that file appears first.

这是怎么回事?我搞砸了配置,还是log4net RollingFileAppender中存在错误?

What's happening here? Have I messed up the config or is there a bug in the log4net RollingFileAppender?

这是我的配置:

<log4net debug="true">
    <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="[log4net] %d [%t] %-5p %l - %m%n" />
        </layout>
    </appender>

    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="log" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <datePattern value="yyyyMMdd-HHmm'.txt'" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>

    <root>
        <level value="INFO" />
        <appender-ref ref="TraceAppender" />
        <appender-ref ref="RollingLogFileAppender" />
    </root>
</log4net>

推荐答案

您正在滚动日期,这意味着只要您指定的datePattern更改,log4net就会滚动日志文件.您已经指定了每分钟更改的日期格式,因此您应该期望每分钟有新的日志滚动.

You're rolling on date, which means that whenever the datePattern you've specified changes, log4net will roll the log file. You've specified a date pattern that changes each minute, so you should expect a new log rolloff each minute.

这些结果将使用您指定的日期模式来命名.但是,活动日志文件将被命名为"log",并在滚存期间重命名.也就是说,使用您的配置log4net将始终写入名为"log"的文件,并在datePattern值更改后将其复制.

These rolloffs will be named using the date pattern you've specified. However the active log file will be named "log" and renamed during rolloff. That is, with your config log4net will always write to the file named "log" and copy it off once the datePattern value changes.

如果我对您的理解正确,那么您希望使用配置文件中的日期模式来命名活动日志文件.试试这个:

If I understand you correctly then you want your active log file to be named using the date pattern in your config. Try this:

 <appender name="RollingFileDateAppender" type="log4net.Appender.RollingFileAppender">
  <file value="log.txt" />
  <appendToFile value="true" />
  <preserveLogFileNameExtension value="true" />
  <staticLogFileName value="false" />

  <rollingStyle value="Date" />
  <datePattern value="yyyyMMdd-HHmm" />

  <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>

staticLogFileName设置控制活动"日志文件的名称;如果为假,log4net将使用datePattern命名活动日志文件.请注意,这意味着无需完成滚存副本,而是将创建一个新的日志文件(具有适当的名称).将preserveLogFileNameExtension设置为true会强制log4net在创建文件时使用.txt文件扩展名;通常,它只会将日期模式附加到文件名的末尾,导致文件扩展名如.txt20130830-0819几乎没有用.

The staticLogFileName setting controls the name of the "active" log file; when it's false, log4net will use the datePattern to name the active log file. Note this means no roll-off copy needs to be done, a new log file (with the appropriate name) will be created instead. Setting preserveLogFileNameExtension to true forces log4net to use the .txt file extension when creating the file; normally it would just append the date pattern to the end of the file name, resulting in a file extension like .txt20130830-0819 which is pretty useless.

这篇关于为什么在使用RollingFileAppender时log4net创建两个单独的日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆