Knowledge.ToString()

RollingFileAppender BufferSize is not Working? Here is an Alternative

If you have used log4net for your logging need, you may have come across the bufferSize property of the appender which will allow you to buffer x number of log entries and then it will flush. It is good for logging performance.

Now you want to use RollingFileAppender and bufferSize is not working at all for you. You are searching internet and pulling your hairs why it is not working even though you would find bufferSize examples with different appenders working without any issue. Here is a sample log4net.config file

<?xml version="1.0"?>
<log4net>
    <appender name="fileAppender" type="log4net.Appender.RollingFileAppender" >
        <file type="log4net.Util.PatternString" value="Log\fontconverter-%date{yyyyMMdd}.resx" />
        <bufferSize value="3" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="ALL"/>
    </root>
    <logger name="myLog">
        <level value="All"></level>
        <appender-ref ref="fileAppender" />
    </logger>
</log4net>

Well, RollingFileAppender does not support bufferSize property and hence it will not work.

Here is an alternative to buffer your logs and then flush it using RollingFileAppender. Instead of using RollingFileAppender, you need to use BufferingForwarder which in turn should use RollingFileAppender to write to log. Here is a working log4net.config file

<?xml version="1.0"?>
<log4net>
    <appender name="BufferingForwarder" type="log4net.Appender.BufferingForwardingAppender">
        <bufferSize value="5" />
        <lossy value="false" />
        <appender-ref ref="fileAppender" />
        <!-- or any additional appenders or other forwarders -->
    </appender>
    <appender name="fileAppender" type="log4net.Appender.RollingFileAppender" >
        <file type="log4net.Util.PatternString" value="Log\fontconverter-%date{yyyyMMdd}.resx" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="ALL"/>
    </root>
    <logger name="myLog">
        <level value="All"></level>
        <appender-ref ref="BufferingForwarder" />
    </logger>
</log4net>

Share

Comments

One response to “RollingFileAppender BufferSize is not Working? Here is an Alternative”

  1. John Wooten, Ph.D. Avatar
    John Wooten, Ph.D.

    I have tried the buffering forwarder. I only buffered things before sending them to the log file appender. That doesn’t write to disk until the end of the day as it is a Roling File Appender. I need the data dumped to disk at least every 5 minutes or 100 to 1000 entries.

Leave a Reply

Your email address will not be published. Required fields are marked *