Wrox Wrox - 1 year ago 154
C# Question

Log4net settings to purge log files

I want to purge log files that are being generated using the Log4net library.

My log4net.config settings are as below:

<file type="log4net.Util.PatternString" value="%property{logPath}" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />

What I want to do is to generate max. 11 files and after that log4net should remove old files.

Answer Source

You need a rolling file appender. See https://logging.apache.org/log4net/release/config-examples.html

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="log.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="100KB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download