3

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.

Philipp M
  • 1,877
  • 7
  • 27
  • 38
G Wood
  • 41
  • 1
  • 2

3 Answers3

0

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).

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • Thanks, I will check into these points. I suspect they are not the reason for the duplicate log files, since the identical setup does not produce the second files in our Dev and Test environments. – G Wood May 17 '12 at 20:36
  • @GSW Yeah I make no claim that they will fix the problem. However, they shouldn't affect the logging, and if they do, it will likely be in an undesired way. – evanmcdonnal May 17 '12 at 21:16
0

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

sluiper
  • 91
  • 7
0

I had the same exact problem.

I confirm that was a permission issue. In my case the logfiles were generated by two different accounts (from a scheduled task and sometimes by a manual run via console), and in this case the file name had a duplicate date pattern.

After resetting the permissions to both users (the scheduled process user and the interactive users) the issue did not repeat anymore.

Greets, Michele

Michele
  • 101
  • 1
  • 6
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33557094) – Eyeslandic Jan 06 '23 at 22:11