Search code examples
logginglog4netlog4net-configurationlog4net-appender

Configure log4net RollingFileAppender to roll on size but append datetime?


I want to configure my log4net Rolling log appender such that the name of my initial log file is:-

log_{current_datetime}.log

When the file reaches 25MB size , a new log file should be created with the same format and the previous log file should not be renamed.

How do I do such a setting on log4net appender?


Solution

  • Use a PatternString for the filename:

    <file type="log4net.Util.PatternString" value="log_%date{yyyyMMdd}.log" />

    In order to configure rolling on size, make sure you specify RollingStyle and MaximumFileSize:

    <rollingStyle value="Size"/>
    <maximumFileSize value="25MB"/>
    

    Full examples can be found here.

    Update:

    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.