Log4Net RollingFileAppender generates duplicate logs

故事扮演 提交于 2019-12-08 05:13:31

问题


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.


回答1:


Your date pattern shouldn't have .log after it. I also am unsure why you have two appenders declared in the root. You should be able to get rid of the root altogether, given the rest of your config it has no purpose (assuming you don't have more that isn't in the example).




回答2:


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



来源:https://stackoverflow.com/questions/10639682/log4net-rollingfileappender-generates-duplicate-logs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!