Log4net preserveLogFileNameExtension not working

回眸只為那壹抹淺笑 提交于 2019-12-23 15:10:05

问题


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>

回答1:


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...



来源:https://stackoverflow.com/questions/27276647/log4net-preservelogfilenameextension-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!