问题
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:
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.
来源:https://stackoverflow.com/questions/16858935/appending-date-to-filename-when-rolling-by-size