0

I'd like to set up a RollingFileAppender in log4net such that the current (i.e. today's) log file always has a static name (like app.log), but upon roll over at the end of the day, it should be renamed to app.<date>.log. Here's as close as I've got so far (note that I'm using every-minute rollover rather than every-day rollover since this is easier to debug):

<appender name="applog" type="log4net.Appender.RollingFileAppender">
  <file value="app.log" />
  <staticLogFileName value="false" />
  <datePattern value=".yyyy-MM-dd-hh-mm" />
  <preserveLogFileNameExtension value="true" />
  <appendToFile value="true" />
  <rollingStyle value="Date" />
  <maxSizeRollBackups value="5" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
  </layout>
</appender>

The problem with this is that I see the following when a request begins:

  • app.2016-02-01-05-00.log

And by the time the request ends, I have these files:

  • app.2016-02-01-05-00.log
  • app.2016-02-01-05-00.log.2016-02-01-05-00.log

Notice that the minute hasn't rolled over yet, but it appears to have created a rollover file of some kind anyway. Also, today's file isn't ever called just 'app.log' as I want, it always starts with the timestamp in the name. Lastly, it doesn't appear to honor my maxSizeRollBackups of 5, as far as I can tell the backups grow indefinitely without ever getting deleted.

I tried removing the staticLogFileName tag, and that makes today's name 'app.log' like I want, but then it rolls over in place, overwriting itself and not creating backup files.

gzak
  • 3,908
  • 6
  • 33
  • 56

1 Answers1

0

After breaking down and downloading the source code, it turns out to be a permission issue with the rollover's System.IO.File.Move() call. I needed to set the folder's Modify permission to true as well, not just Read and Write (which is strange, because isn't a move technically a write operation?).

I also discovered that you should NOT set staticLogFileName to false, so I had to remove that element from the xml.

gzak
  • 3,908
  • 6
  • 33
  • 56
  • I am facing the same issue that file name gets appended with current date time when its gets updated as like the below abc-29-11-2017-04-0429-11-2017-04-04 How to resolve it.? Please let me know if there is any solution for this? – mRhNs13 Nov 29 '17 at 10:34