Click here to Skip to main content
13,349,502 members (49,729 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 13 Jun 2007

Create Multiple Sink Files with Log4net

, 17 Jul 2007
Rate this:
Please Sign up or sign in to vote.
How to create multiple sink files with Log4net


MyLogger library enables you to log the level of errors/events in distinct files. To use MyLogger in your project, add the reference into your project. You will also need to add a reference to Log4net, as MyLogger is nothing but a simple wrapper round Log4net. To start the Logger, place the following code in the start of your application:

Logger.loggerFileName = "LogFile"; 

Once the method StartLoggingThread is called, it starts the logger thread. Here in MyLogger, I have included two levels of logging, i.e. Error and Trace, although it can be increased to the same number as provided by Log4net. To log an error, write:

int i = 10; 
int j = 0; 
int k = i / j; 
catch (Exception ex) 
Logger.LogMessage("An Error Occurred : " + ex.Message, Logger.LOG_ERROR); 

The Logger.LOG_ERROR property lets the MyLogger class know that it is an error and needs to be logged in the error file. Similarly, if you want to trace something, use Logger.LOG_TRACE. For example:

Logger.LogMessage("Reading from File", Logger.LOG_TRACE); 

Remember to ShutDown the logger when you are closing the application. You can write this code in the Application Exit event for your application:



The above section explains how to use MyLogger in your application. Now I'll explain how MyLogger has been designed and how you can enhance it for further use.

MyLogger runs on a separate thread, thus enabling the application to respond in a comparatively faster fashion and not wait while Log4net logs in the event. This thread becomes active when an event occurs which is notified by the ManualResetEvent.

Currently, there are only two appenders created while MyLogger starts up, but you can modify and add more appenders as per your need. The Logger.LOG_ERROR property lets the MyLogger class know that it is an error and needs to be logged in the error file. The public property loggerFileName is used to set the output filename.


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


About the Author

Manas Bhardwaj
Netherlands Netherlands

Read my personal blog at

You may also be interested in...


Comments and Discussions

GeneralHi Pin
Xs_213-Jun-07 5:36
memberXs_213-Jun-07 5:36 
Im looking for an example on a custom appendor im trying to build an sms appendor that sends an sms to a mobile phone can anyone help me please ??
Thank You in advance? Confused | :confused:

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 17 Jul 2007
Article Copyright 2007 by Manas Bhardwaj
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid