Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Article

Create Multiple Sink Files with Log4net

Rate me:
Please Sign up or sign in to vote.
3.62/5 (11 votes)
17 Jul 2007CPOL2 min read 30K   308   17   1
How to create multiple sink files with Log4net

Introduction

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:

C#
Logger.loggerFileName = "LogFile"; 
Logger.StartLoggingThread();

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:

C#
try 
{ 
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:

C#
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:

C#
Logger.ShutDownLogger(); 

Details

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.

License

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


Written By
Architect
Netherlands Netherlands

Read my personal blog at www.manasbhardwaj.net.


Comments and Discussions

 
GeneralHi Pin
Cape Town Developer13-Jun-07 4:36
Cape Town Developer13-Jun-07 4: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.