Tuesday, May 10, 2011

Remote Log4Net

You probably know this already, or this might not come as a surprise but enabling log4net to log to a remote place is really simple.

We had a scenario of only one front end with custom systems using log4net, but we needed to add a second front end, and with that the question about what to do about logging.

I wasn't aware how simple is to enable remote logging on log4net. So, heads up if you were like me:

Get log4net Remove Logging Service at CodePlex (you can get the source and change at your will), after install this will create a listener service where you want to store your remote log repository.

Then, at your local clients just change the appender to something like this

<appender name="remotAppend" type="log4net.Appender.RemotingAppender" >
<sink value="tcp://remoteserver:port/Log4netRemotingServerService" />
<lossy value="false" />
<bufferSize value="10" />
<onlyFixPartialEventData value="true" />
</appender>

At your Remote repository use your favorite appender, RollingLogFileAppender or whatever.
Remember to add the hostname property to your layout type: (%-10property{log4net:HostName}).

Something like this:

<appender name="RollLogAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\LOGS\MYAPP.log.txt" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<encoding value="UTF-8" />
<datePattern value="'.'yyyy'.'MM'.'dd'.'HH'.log.txt'" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date (%-10property{log4net:HostName}) [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>

Maybe you could do without this Codeplex service, but since it was so simple and the component is working fine I didnt waste any time looking for alternatives.

If you dont agree, please comment, shed some light :) Thank you.

No comments: