Click here to Skip to main content
14,332,804 members

Mini Drop-in Replacement for log4net

Rate this:
4.83 (32 votes)
Please Sign up or sign in to vote.
4.83 (32 votes)
4 May 2012CPOL
Mini drop-in replacement for log4net


This is a simple 8KB drop in replacement for complex 198KB Log4net that’s a 96% size reduction. Strange that after looking on the web for a logging solution that fitted my requirements, I couldn't find one.


I have been using log4net for the past 7 years and it is undoubtedly the de-facto standard for logging in the .NET Framework environment. I am also a follower of the minimalistic philosophy, and to this end, I have created a drop in replacement for log4net which is smaller and a lot less complex.

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.

Obviously, it does not have the feature set of the original, but 99% of the time you don't need it in my experience.

What You Get

So what this mini log4net does for you is the following:

  • 200 lines of code
  • Drop-in replace log4net
  • Threaded logger: no blocking of main code to log messages
  • Write to a text files only (you can add other destinations yourself if you need it)
  • Size limit log files and roll the file names with a count number
  • Date roll log files: new file for each new day
  • Simple single method configuration: no XML configuration files
  • Ability to log method name: *performance hit

Using the Code

You can pretty much forget about any changes to your code because it will work as is.

public class someclass
    log4net.ILog _log = LogManager.GetLogger(typeof(someclass));

    public void Method()
       _log.Debug("hello world!");

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.

public static void Main()
              //where to put the logs : folders will be automatically created
               500,// limit the file sizes to 500kb, 0 = no limiting
               false); // log method call names -> performance hit if enabled


Points of Interest

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:

  • AppDomain.CurrentDomain.ProcessExit: For this function to work, you must set the thread IsBackground to True 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.
  • Rolling filenames: If you look at the code for implementing the rolling functionality for file names, it’s simple and dirty but it works.
  • LOG format output: The log output format is hardcoded to my own preference but you can change it to what you like in the code.
  • Size limit: 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.
  • Method names: If you set the showmethodnames 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.

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, it doesn’t matter.


  • Initial release: 2010/12/07 
  • Update v1.1 : 2012/05/05
    • uses a timer instead of a thread
    • added Shutdown() for implicit closing of files
    • optimized string output


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


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 Platinum's on Code-Project (13th Jan'12)
* Mehdi is the 3rd person to get 7 out of 7 Platinum's on Code-Project (26th Aug'16)

Comments and Discussions

GeneralMy vote of 1 Pin
varun1501-Jun-15 3:10
membervarun1501-Jun-15 3:10 
Questionwhere you have set Isbackground =true in sample ??,,pooor explaination./ Pin
varun1501-Jun-15 3:09
membervarun1501-Jun-15 3:09 
AnswerRe: where you have set Isbackground =true in sample ??,,pooor explaination./ Pin
Mehdi Gholam1-Jun-15 3:25
mveMehdi Gholam1-Jun-15 3:25 
GeneralRe: where you have set Isbackground =true in sample ??,,pooor explaination./ Pin
varun1501-Jun-15 19:44
membervarun1501-Jun-15 19:44 
GeneralRe: where you have set Isbackground =true in sample ??,,pooor explaination./ Pin
Mehdi Gholam1-Jun-15 19:56
mveMehdi Gholam1-Jun-15 19:56 
GeneralRe: where you have set Isbackground =true in sample ??,,pooor explaination./ Pin
varun1501-Jun-15 23:44
membervarun1501-Jun-15 23:44 
GeneralRe: where you have set Isbackground =true in sample ??,,pooor explaination./ Pin
Mehdi Gholam1-Jun-15 23:52
mveMehdi Gholam1-Jun-15 23:52 
GeneralRe: where you have set Isbackground =true in sample ??,,pooor explaination./ Pin
varun1501-Jun-15 23:58
membervarun1501-Jun-15 23:58 
QuestionMissing AM/PM Pin
SohailB20-May-15 8:49
memberSohailB20-May-15 8:49 
AnswerRe: Missing AM/PM Pin
Mehdi Gholam20-May-15 9:53
mveMehdi Gholam20-May-15 9:53 
GeneralMy vote of 5 Pin
MarkBoreham26-Jun-14 9:04
professionalMarkBoreham26-Jun-14 9:04 
GeneralMy vote of 5 Pin
Mazen el Senih11-Jul-13 3:34
professionalMazen el Senih11-Jul-13 3:34 
GeneralMy vote of 5 Pin
umta13-Jun-13 5:42
professionalumta13-Jun-13 5:42 
QuestionCurrent Version Problems Pin
Dewey28-Jul-12 12:47
memberDewey28-Jul-12 12:47 
AnswerRe: Current Version Problems Pin
Mehdi Gholam28-Jul-12 17:26
mveMehdi Gholam28-Jul-12 17:26 
Questioncan minilog4net be used in such condition? Pin
Alenty4-Jun-12 14:21
memberAlenty4-Jun-12 14:21 
AnswerRe: can minilog4net be used in such condition? Pin
Mehdi Gholam4-Jun-12 18:56
mveMehdi Gholam4-Jun-12 18:56 
GeneralMy vote of 5 Pin
HoyaSaxa9316-May-12 3:51
memberHoyaSaxa9316-May-12 3:51 
GeneralRe: My vote of 5 Pin
Mehdi Gholam4-Jun-12 18:57
mveMehdi Gholam4-Jun-12 18:57 
QuestionA very small improvement for performance Pin
wmjordan11-May-12 15:05
professionalwmjordan11-May-12 15:05 
AnswerRe: A very small improvement for performance Pin
Mehdi Gholam11-May-12 18:10
mveMehdi Gholam11-May-12 18:10 
GeneralRe: A very small improvement for performance Pin
wmjordan12-May-12 16:39
professionalwmjordan12-May-12 16:39 
QuestionWhy moving to a timer? Pin
Dewey5-May-12 17:02
memberDewey5-May-12 17:02 
AnswerRe: Why moving to a timer? Pin
Mehdi Gholam5-May-12 20:09
mveMehdi Gholam5-May-12 20:09 
GeneralRe: Why moving to a timer? Pin
Dewey14-May-12 15:49
memberDewey14-May-12 15:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 13 Dec 2010

Tagged as


76 bookmarked