Log4net preserveLogFileNameExtension not working
Asked Answered
B

1

5

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>
Bastogne answered 3/12, 2014 at 16:21 Comment(4)
It may be conflcting with staticLogFileName which I don't think you want in this scenario.Faze
I tested by removing the staticLogFileName but still not working..Bastogne
What version are you using? Also, can you try using <layout type="log4net.Layout.PatternLayout">? Outside of changing that to fix an log4net exception, this works for me on version 1.2.13.Bale
What is your log4net version?Hyson
M
3

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 the staticLogFilename property.

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 preserveLogFileNameExtension property for a RollingFileAppender (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 component


When 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 maxSizeRollBackups and countDirection values). You cannot avoid this with the current RollingFileAppender (take a look at the RollingFileAppender.RollOverRenameFiles method)

What to do? You can decide to roll by date. In this case it is possible to use the datePattern property. The datePattern is appended to the file name when rolling, so you can use something like

<file value="file.log" />
<datePattern value=".yyyy-MM-dd-hh-mm-ss'.log'" />

which 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; since RollOverSize always occur after RollOverDate the 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 RollingFileAppender and only override necessary method. AdjustFileBeforeAppend is virtual and is the starting point for all rollovers needs; you could try for example to reverse the calls to RollOverDate and RollOverSize to use the Composite mode...

Mares answered 11/12, 2014 at 10:54 Comment(2)
You may be right 1.2.10, but not for the current version (1.2.13): logging.apache.org/log4net/release/sdk/… Updating is strongly recommended, because there are actually some nasty bugs with rolling files that are fixed between 1.2.10 and 1.2.11.Heilner
@JeroenMostert Thanks for the heads up. I didn't think there were some changes on this part in the minor versions.Mares

© 2022 - 2024 — McMap. All rights reserved.