4
votes

I'd like to configure log4net to append the name to the filenames when rolling by size. I've only played around with the configuration section of log4net, not sure if I have to configure something in code for this.
This is what I have now:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="C:\\rolling-log.txt" />
  <appendToFile value="true" />
  <datePattern value="_yyyy-MM-dd" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10KB" />
  <rollingStyle value="Size" />
  <staticLogFileName value="true" />
  <preserveLogFileNameExtension  value="true"/>
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%m%n" />
  </layout>
</appender>

I can only get the date in the filename if the rollingStyle is set to Date, but then it doesn't roll on size anymore.

What am I missing here?

1
Well of course not. If you roll by size, the date doesn't matter. The log can be multiple days, or multiple files can be for one day.Jeremy Holovacs
Well..I'd like to have something like log2013-31-5_1.log, log2013-31-5_2.log, log2013-1-6_1.log, etc. I'm not asking too much, am I? :)Adrian
well, yeah, you are. How you would want to do that is completely different from how someone else might want to do that, so now you are asking for some means of defining not only a naming convention, but business logic for naming the log files, and integrate it into how they are rolled over. I think you can write your own mechanism for that, but it's certainly above and beyond the standard purpose of a logging library. If you want more granular control, I suggest logging to a database instead.Jeremy Holovacs
Thanks for your suggestions Jeremy, but using a database is not the point here. I was just looking for a way to control the log file name and it doesn't involve a lot of business logic. Thanks anyway.Adrian

1 Answers

-1
votes

Change

<file value="C:\\rolling-log.txt" />

to

<file type="log4net.Util.PatternString" value="C:\\rolling-log-%date{yyy-MM-dd}.txt" />

(or whatever date pattern you prefer).

Caveat:

Unfortunately, the date specified in the filename is only evaluated upon configuration, meaning the same date will be used on each roll until log4net is reconfigured. I have not found a way to fix this.