25

I am using the RollingFileAppender and the Size rollingStyle. By default it creates backup files with a numbered extension, this drives me nuts. Is it possible to change it so it always uses a defined extension (say .txt or .log) and inserts the number as part of the file name?

For example:

myapp.log
myapp.1.log
myapp.2.log
myapp.3.log

Here is my current configuration:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="myapp.log"/>
    <appendToFile value="true"/>
    <rollingStyle value="Size"/>
    <maximumFileSize value="1MB"/>
    <maxSizeRollBackups value="10"/>
    <staticLogFileName value="true"/>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{ISO8601} [%3thread] %-5level %logger{3}: %message%newline" />
    </layout>
</appender>
Philipp M
  • 1,877
  • 7
  • 27
  • 38
BrettRobi
  • 3,793
  • 7
  • 40
  • 64

1 Answers1

36

The PreserveLogFileNameExtension property set to true should do the trick. Note: this property is not available in the currently released version 1.2.10 but is part of the current source. If you grab and build the source you're good to go.

Update: it is great to see log4net is moving forward. Version 1.2.11, and with it the PreserveLogFileNameExtension property, have been released.

David Gardiner
  • 16,892
  • 20
  • 80
  • 117
Peter Lillevold
  • 33,668
  • 7
  • 97
  • 131
  • Where does one get the current source for log4net. When I download the source from here: http://logging.apache.org/log4net/download.html it does not appear to have the PreserveLogFileNameExtension support. Do you think I can take the RollingFileAppender.cs you link to above and overlay it on my 1.2.10 source successfully? – BrettRobi Apr 13 '10 at 14:47
  • You'll find the repository here: http://logging.apache.org/log4net/source-repository.html. – Peter Lillevold Apr 13 '10 at 15:22
  • How can I achieve the same behavior without having to patch log4net, i.e. by extending log4net? – bitbonk Jul 10 '11 at 21:18
  • @bitbonk what do you mean by "patching"? The latest release (1.2.10) is rather old, but the functionality is in the current source. You only have to bring down the source and compile the dll yourself and you're good to go. – Peter Lillevold Jul 10 '11 at 21:41
  • 2
    Log4net's latest version is 1.2.11. You can download it in http://logging.apache.org/log4net/download.html – jean27 Nov 22 '11 at 08:38
  • 3
    annoyingly PreserveLogFileNameExtension doesn't work with which means that you can't preserve file extensions and have the current log file without a date stamp in the name – Matthew Lock Dec 21 '12 at 05:22
  • @MatthewLock: you're saying that with staticLogFileName+PreserveLogFileNameExtension you will still get `file.log.somedatestamp` ? – Peter Lillevold Dec 21 '12 at 12:29