Interesting logging alternative (and bonus)
Almost every application needs to store all the errors, debugging
outputs, warnings, audits and etc for the usual needs.
It's common to develop some kind of a
library that will enable us to write all the logged information in one (or
more) repository the common ones are simple files and even log entries.
There are two dominating open source libraries – one, more common in my
opinion, from Microsoft (Logging
Application Block). And one from apache java port log4j – log4net.
I will focus on log4net (Logging Application Block has a lot of
documentation and examples through the cyber space).
I'll briefly cover some of log4net feature:
Appender's are custom "writing class" for various repository
types, such as:
·
ASP.NET Trace
·
ADO.NET output
·
Colored console
·
Debug
·
Event log
·
File
·
Rolling file (the name
changes)
·
SMTP
·
Telnet
·
Remoting
·
And more
Each log "message" can be layouted in various ways:
·
Raw layout
·
XML layout
·
Simple layout
·
Pattern layout
·
And more
In addition log4net supports active filtering, multiple appenders,
ability to modify configuration in runtime with closing the process (internally
using FileSystemWatch) and more.
In my opinion it worth to check out this framework...
As promissed the *BONUS* - I wrote another appender that I believe many can use and benefit from it
– I called it MultiThreadedBufferedAppender and it supports:
·
Buffered output (x writes
before actual flush to file)
·
Rolling files with optional
process name before the specified output file name
·
Automatic flushing thread
(in case you are in the middle of the buffer)
Configuration:
<appender name="MultiThreadedBufferedAppender" type="log4net.Appender.MultiThreadedBufferedAppender">
<file value="log.txt"/>
<AutoFlashAfterXSeconds
value="60"/>
<InsertProcessName
value="true"/>
<NumberOfFlushesBeforeRollover
value="100"/>
<LinesToBufferBeforeFlush
value="1000"/>
<layout
type="log4net.Layout.PatternLayout">
<header
value="Date,Process,Level,Category,Message"/>
<conversionPattern value="%date{yyyy-MM-dd
HH:mm:ss:fff},%processname,%level,%category,"%message"%newline"/>
</layout>
</appender>
In the attached
file you'll have 3 files – FileAppender (I needed to extern it's private stream
class and stream instance), MultiThreadedBufferedAppender.cs (the appender himself) and MultiThreadedBufferedAppenderTest.cs
(the nunit tests for the appender). You'll need to download log4net from here (I used
v1.2.10).
Download
the appended.
Have fun