this is my log4net configuration, the rollover log file is created wrong extension. The first file created with name of log_debug.txt and the rollover file created with log_debug.txt.1. but ideally it should be log_debug.1.txt.
I used preserveLogFileNameExtension value to be true, but it seems not working. Can you please check and let me know if anything wrong?
<appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="DEBUG" />
</filter>
<file value=".\logs\log_debug.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="20" />
<maximumFileSize value="2MB" />
<preserveLogFileNameExtension value="true" />
<staticLogFileName value="true" />
<layout type="propertyPatternLayout">
<conversionPattern value="%date || Thread=%thread" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
My first answer (below) was based on v1.2.10 of log4net and I was gently prodded towards the v1.2.13 version which does contain the
preserveLogFileNameExtension. Some tests confirmed that the property also works as intended, and is not dependent on thestaticLogFilenameproperty.I think that OP is using a previous version of log4net that doesn't contain the property and thus exhibits the behavior described below (making3 and Sivakumar comment are on the spot IMO)
Thank again for waking me out of the 1.2.10 groove.
Initial answer
First thing first: there is no
preserveLogFileNameExtensionproperty for aRollingFileAppender(or any appender) in vanilla log4net (I'm looking at version 1.2.10.0). Most log4net components will consume any property configuration you send their way without complaining but it doesn't necessarily translate to a behavior in the componentWhen rolling on size (or composite), what happens is that the rolling over is hardcoded to suffix the log file with the iteration number. You will always get a "file.log.n" with n matching the next file number (which depends on your
maxSizeRollBackupsandcountDirectionvalues). You cannot avoid this with the currentRollingFileAppender(take a look at theRollingFileAppender.RollOverRenameFilesmethod)What to do? You can decide to roll by date. In this case it is possible to use the
datePatternproperty. ThedatePatternis appended to the file name when rolling, so you can use something likewhich would preserve the extension and create a file like
file.log.2014-12-11-11-47-54.log(see the original .log extension in the file name). But you are losing the size limitation aspect. if you go for Composite you will encounter the same problem; sinceRollOverSizealways occur afterRollOverDatethe number prefix will be present.You can also decide to roll out (ah ah) your own appender with the naming conventions you want/need. You could even try to inherit from the vanilla
RollingFileAppenderand only override necessary method.AdjustFileBeforeAppendis virtual and is the starting point for all rollovers needs; you could try for example to reverse the calls toRollOverDateandRollOverSizeto use the Composite mode...