Click here to Skip to main content
11,712,466 members (75,676 online)
Click here to Skip to main content
Add your own
alternative version

Mini Drop-in Replacement for log4net

, 4 May 2012 CPOL 45.7K 1.4K 66
Mini drop-in replacement for log4net
<html>

<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1256">
<meta name=Generator content="Microsoft Word 14 (filtered)">

</head>

<body lang=EN-US link=blue vlink=purple>

<div class=WordSection1>

<p  style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Courier New"'>Title:������
mini Log4net<br>
Author:����� mgholam</span></p>

<p  style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Courier New"'>Email:������ <a
href="mailto:mgholam@hotmail.com%20Member">mgholam@hotmail.com </a></span></p>

<p  style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Courier New"'><a
href="mailto:mgholam@hotmail.com%20Member"><span style='color:windowtext;
text-decoration:none'>Member</span></a> ID:�� 151481� </span></p>

<p  style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Courier New"'>Language:���
C#</span></p>

<p  style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Courier New"'>Platform:���
Windows</span></p>

<p  style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Courier New"'>Technology:�
.net</span></p>

<p  style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Courier New"'>Level:������
Intermediate</span></p>

<p  style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Courier New"'>Description:
mini drop-in replacement for log4net</span></p>

<p  style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Courier New"'>Section:����� </span></p>

<p  style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Courier New"'>SubSection:�� </span></p>

<p  style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Courier New"'>License:����
CPOL</span></p>

<p  style='line-height:normal'><b><span style='font-size:18.0pt;
font-family:"Times New Roman","serif"'>Introduction</span></b></p>

<p  style='line-height:normal'><span style='font-size:12.0pt;
font-family:"Times New Roman","serif"'>This is a simple 8kb drop in replacement
for complex 198kb Log4net that�s a 96% size reduction.&nbsp;&nbsp;Strange that
after looking on the web for a logging solution that fitted my requirements I
couldn't find one.</span></p>

<p  style='line-height:normal'><b><span style='font-size:18.0pt;
font-family:"Times New Roman","serif"'>Background</span></b></p>

<p  style='line-height:normal'><span style='font-size:12.0pt;
font-family:"Times New Roman","serif"'>I have been using log4net for the past 7
years and it is undoubtedly the de-facto standard for logging in&nbsp;the .net
framework environment. I am also&nbsp;a follower of the minimalistic
philosophy, and to this end I have created a drop in replacement for log4net
which is smaller and&nbsp;a lot less&nbsp;complex. </span></p>

<p  style='line-height:normal'><span style='font-size:12.0pt;
font-family:"Times New Roman","serif"'>This was done mostly for the reason of
size restrictions in a project i.e. minimizing the deployment footprint of the
app and also as a review exercise. </span></p>

<p  style='line-height:normal'><span style='font-size:12.0pt;
font-family:"Times New Roman","serif"'>Obviously it does not have the feature
set of the original, but 99% of the time you don't need it in my
experience.&nbsp;</span></p>

<p  style='line-height:normal'><b><span style='font-size:18.0pt;
font-family:"Times New Roman","serif"'>What you get</span></b></p>

<p  style='line-height:normal'><span style='font-size:12.0pt;
font-family:"Times New Roman","serif"'>So what does this mini log4net do for
you is the following:</span></p>

<ul type=disc>
 <li  style='line-height:normal'><span style='font-size:12.0pt;
     font-family:"Times New Roman","serif"'>200 lines of code</span></li>
 <li  style='line-height:normal'><span style='font-size:12.0pt;
     font-family:"Times New Roman","serif"'>Drop-in replace log4net</span></li>
 <li  style='line-height:normal'><span style='font-size:12.0pt;
     font-family:"Times New Roman","serif"'>Threaded logger : no blocking of
     main code to log messages</span></li>
 <li  style='line-height:normal'><span style='font-size:12.0pt;
     font-family:"Times New Roman","serif"'>Write to a text files only ( you
     can add other destinations yourself if you need it)</span></li>
 <li  style='line-height:normal'><span style='font-size:12.0pt;
     font-family:"Times New Roman","serif"'>Size limit log files and roll the
     file names with a count number</span></li>
 <li  style='line-height:normal'><span style='font-size:12.0pt;
     font-family:"Times New Roman","serif"'>Date roll log files: new file for
     each new day</span></li>
 <li  style='line-height:normal'><span style='font-size:12.0pt;
     font-family:"Times New Roman","serif"'>Simple single method configuration
     : no xml configuration files</span></li>
 <li  style='line-height:normal'><span style='font-size:12.0pt;
     font-family:"Times New Roman","serif"'>Ability to log method name:
     *performance hit</span></li>
</ul>

<p  style='line-height:normal'><b><span style='font-size:18.0pt;
font-family:"Times New Roman","serif"'>Using the code</span></b></p>

<p  style='line-height:normal'><span style='font-size:12.0pt;
font-family:"Times New Roman","serif"'>You can pretty much forget about any
changes to your code because it will work as is. </span></p>

<p  style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Courier New"'>public class
someclass<br>
{<br>
&nbsp;&nbsp;&nbsp; log4net.ILog _log = LogManager.GetLogger(typeof(someclass));<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp; public void Method()<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
_log.Debug(&quot;hello&nbsp;world!&quot;);<br>
&nbsp;&nbsp;&nbsp; }<br>
}</span></p>

<p  style='line-height:normal'><span style='font-size:12.0pt;
font-family:"Times New Roman","serif"'>The only changes to your code would be
in the startup routine for your application where you configure the logger,
this would be where you use log4net's DOMConfigurator methods and the xml
configuration files.</span></p>

<p  style='margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal'><span style='font-size:10.0pt;font-family:"Courier New"'>public static
void Main()<br>
{<br>
&nbsp;&nbsp; LogManager.Configure(<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&quot;LOGS\\log.txt&quot;, //where to put the logs : folders will be automatically
created<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;500,
// limit the file sizes to 500kb, 0 = no limiting<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
false); // log method call names -&gt; performance hit if enabled<br>
}</span></p>

<p  style='line-height:normal'><b><span style='font-size:18.0pt;
font-family:"Times New Roman","serif"'>Points of Interest</span></b></p>

<p  style='line-height:normal'><span style='font-size:12.0pt;
font-family:"Times New Roman","serif"'>The code is pretty straightforward given
it is only 200 lines long, but there is a couple of �gotchas� that I will point
out here:</span></p>

<p  style='text-indent:-.25in;line-height:normal'><span
style='font-size:12.0pt;font-family:Symbol'>�<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span dir=LTR></span><b><span style='font-size:12.0pt;font-family:
"Times New Roman","serif"'>AppDomain.CurrentDomain.ProcessExit</span></b><span
style='font-size:12.0pt;font-family:"Times New Roman","serif"'>: for this
function to work you must set the thread <i>IsBackground</i> to <i>True</i>
otherwise it will just ignore the process exit and continue the thread. This
point took me around 4 hours and a lot of hassle to try to figure out.</span></p>

<p  style='text-indent:-.25in;line-height:normal'><span
style='font-size:12.0pt;font-family:Symbol'>�<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span dir=LTR></span><b><span style='font-size:12.0pt;font-family:
"Times New Roman","serif"'>Rolling filenames</span></b><span style='font-size:
12.0pt;font-family:"Times New Roman","serif"'>: if you look at the code for
implementing the rolling functionality for file names it�s simple and dirty but
it works.</span></p>

<p  style='text-indent:-.25in;line-height:normal'><span
style='font-size:12.0pt;font-family:Symbol'>�<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span dir=LTR></span><b><span style='font-size:12.0pt;font-family:
"Times New Roman","serif"'>LOG format output</span></b><span style='font-size:
12.0pt;font-family:"Times New Roman","serif"'>: the log output format is
hardcoded to what my own preference but you can change it to what you like in
the code.</span></p>

<p  style='text-indent:-.25in;line-height:normal'><span
style='font-size:12.0pt;font-family:Symbol'>�<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span dir=LTR></span><b><span style='font-size:12.0pt;font-family:
"Times New Roman","serif"'>Size limit</span></b><span style='font-size:12.0pt;
font-family:"Times New Roman","serif"'>: the size limitation for files is not
exact but it does the job in the least lines of code and keeps the message
contiguous, so you don�t have to open before and after files to see the
messages.</span></p>

<p  style='text-indent:-.25in;line-height:normal'><span
style='font-size:12.0pt;font-family:Symbol'>�<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span dir=LTR></span><b><span style='font-size:12.0pt;font-family:
"Times New Roman","serif"'>Method names</span></b><span style='font-size:12.0pt;
font-family:"Times New Roman","serif"'>: if you set the <i>showmethodnames</i>
in the Configuration function the logger will output the method name for the
function in the log file which is a great help for debugging code as it
specifies the exact place of the message/error and you don�t have to do
anything. <br>
The only problem is the performance hit you get because the code does a stack
trace and extracts the method name. I would not recommend using this in
production code but then again if you don�t log that many messages, doesn�t
matter.</span></p>

<p  style='line-height:normal'><span style='font-size:12.0pt;
font-family:"Times New Roman","serif"'>&nbsp;</span></p>

<p  style='line-height:normal'><b><span style='font-size:18.0pt;
font-family:"Times New Roman","serif"'>History</span></b></p>

<p  style='line-height:normal'><span style='font-size:12.0pt;
font-family:"Times New Roman","serif"'>Initial Release : 2010/12/07</span></p>

<p >&nbsp;</p>

</div>

</body>

</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mehdi Gholam
Architect
United Kingdom United Kingdom
Mehdi first started programming when he was 8 on BBC+128k machine in 6512 processor language, after various hardware and software changes he eventually came across .net and c# which he has been using since v1.0.
He is formally educated as a system analyst Industrial engineer, but his programming passion continues.

* Mehdi is the 5th person to get 6 out of 7 Platinums on CodeProject (13th Jan'12)

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 5 May 2012
Article Copyright 2010 by Mehdi Gholam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid