RollingFileAppender bufferSize is not working? Here is an alternative

By | February 8, 2017

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>
Vishal Monpara is a full stack Solution Developer/Architect with 12 years of experience primarily using Microsoft stack. He is currently working in Retail industry and moving 1’s and 0’s from geographically dispersed hard disks to geographically dispersed user’s mind leveraging geographically dispersed team members.

Leave a Reply

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