Log4Net RollingFileAppender生成重复日志 [英] Log4Net RollingFileAppender generates duplicate logs

查看:923
本文介绍了Log4Net RollingFileAppender生成重复日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一台服务器上运行WCF服务,使用Log4net通过INFO和WARN级日志条目跟踪使用情况。使用RollingFileAppender与以下非常标准的配置:

 < appender name =RollingLogFileAppender = log4net.Appender.RollingFileAppender > 
< file value =\\mylocation\data\PRD\myApp\MyService/>
< appendToFile value =true/>
< rollingStyle value =Date/>
< datePattern value = - yyyy-MM-dd'.log'/>
< staticLogFileName value =false/>
< layout type =log4net.Layout.PatternLayout>
< conversionPattern value =%date [%thread]%-5level%logger - %message%newline/>
< / layout>
< / appender>
< root>
< level value =ALL/>
< appender-ref ref =RollingLogFileAppender/>
< appender-ref ref =ADONetAppender_SqlServer/>
< / root>

我还使用ADONetAppender,它接收重定向的WARN级数据并将其写入DB表在SQL Server中通过存储过程。这个配置有点长,所以我省略了它的可读性。



我已经在我们的Dev和TST环境中运行了这个设置。在PRD环境中,似乎生成了重复的日志文件。第一个根据我指定的模式命名,即logfile-yyyy-mm-dd.log。第二个文件看起来像第一个文件,日期模式重复,即logfile-yyyy-mm-dd.log.-yyyy-mm-dd.log。



使这更有趣的是,两个文件中包含的条目随时间重叠。文件1可能有从上午8点到12点的条目,文件2也将包含相同时间段内的条目。这些条目不是重复的,它们由服务的不同用户生成。文件1和2的副本几乎可以是任何大小,因此这不是达到大小或日期/时间阈值并生成下一个所需日志文件的问题。



DB表条目包含所有预期的行,其中一些包含在每个日志文件中。这些行仅通过WARN级别日志记录生成,并且每个日志文件中都会出现一些警告。



我已经从我们店里的一些log4net精灵中退出了这些行,但没有人一个很好的想法,可能导致这个重复的文件行为。

解决方案

我发现,当记录的文件被另一个线程锁定时,会发生这种情况,过程。



我假设Log4Net创建另一个文件,因为它无法登录到配置的日志文件,因此创建一个新文件并记录到其中, d必须通过log4net代码来确定这个假设。



尝试添加

 < lockedModel type =log4net.Appender.FileAppender + MinimalLock/> 

到appender元素以最大限度地减少锁定量。请注意,使用MinimalLock有额外的开销: http://logging.apache.org/log4net/release/sdk/log4net.Appender.FileAppender.MinimalLock.html


I have a WCF service running on a single server, using Log4net to track usage via INFO and WARN level log entries. Using a RollingFileAppender with the following very standard config:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="\\mylocation\data\PRD\myApp\MyService"/>
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="-yyyy-MM-dd'.log'" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
   <root>
      <level value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
      <appender-ref ref="ADONetAppender_SqlServer" />
    </root> 

I also use an ADONetAppender, which receives redirected "WARN" level data and writes it to a DB table in SQL server via a stored procedure. The config for this is a bit long, so I have omitted it for readability.

I have this setup in our Dev and TST environments where it has been running fine. In the PRD environment, it seems to generate duplicate log files. The first is named according to my specified pattern i.e. "logfile-yyyy-mm-dd.log". The second file looks like an addition to the first, with the date pattern duplicated i.e. "logfile-yyyy-mm-dd.log.-yyyy-mm-dd.log".

Making this more interesting is that entries contained in the two files overlap by time. File 1 might have entries from 8am to 12am, and file 2 will also contain entries from the same time period. The entries are not duplicates, they are generated by different users of the service. The copies of Files 1 and 2 can be pretty much any size, so this is not an issue of reaching a size or date/time threshold and generating the next required log file.

The DB table entries contain all the expected rows, some contained in each of the log files. These rows are generated only by WARN level logging, and some WARNings appear in each log file.

I have bounced this off some log4net savvy folks in our shop, but nobody has a good idea of what might be causing this duplicate file behavior. Any ideas from Stackland appreciated.

解决方案

I've found that this happens when the file being logged to is locked by another thread or process.

I'm assuming Log4Net creates the other file because it can't log to the configured logfile and thus creates a new file and logs to it, but I'd have to go through the log4net code to be sure of that assumption.

Try adding

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

to the appender element to minimize the amount of locking. Just be aware that there is additional overhead associated with using MinimalLock: http://logging.apache.org/log4net/release/sdk/log4net.Appender.FileAppender.MinimalLock.html

这篇关于Log4Net RollingFileAppender生成重复日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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