Click here to Skip to main content
13,901,142 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked
Posted 19 Jun 2012
Licenced CPOL

Creating a custom log4net appender

, 19 Jun 2012
Rate this:
Please Sign up or sign in to vote.
A brief example of how to create and implement a custom log4net appender


I had a recent task where I wanted to log events in a section of our code to our content management system. I leveraged log4net, created a custom appender, and was logging successfully in no time.

I used this detailed tutorial to get a better grasp on log4net and I would consider reading it a prerequisite before going through this article. Custom appenders were out of scope of that article so I thought I'd try to give an example here. 

Using the code 

Here are the portions of my web.config with the log4net definitions.  As you can see, I have a logger node which instructs that all code in the ACM.Certification namespace will use the ACMAppender appender for logging with the minimum logging level of INFO. Above the logger node, I have my appender definition node which identifies the type (ACM.ACMAppender) used for the appender.

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

<log4net debug="false">
<appender name="ACMAppender" type="ACM.ACMAppender">
  <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
<logger name="ACM.Certification" additivity="false">
  <level value="INFO" />
  <appender-ref ref="ACMAppender" />

Here is my ACMAppender class which simply implements the abstract class AppenderSkeleton.  It also uses the RenderLoggingEvent() helper method which renders the logging event as a string using the ConversionPattern defined in the web.config above. The LogService object is the custom logging class for my content management system.  You would replace the contents of the <code>Append() method with whatever custom logging you'd want for your implementation.

public class ACMAppender : AppenderSkeleton
    protected override void Append(LoggingEvent loggingEvent)
        LogLevel logLevel = LogLevel.Err;
        switch (loggingEvent.Level.Name)
            case "DEBUG":
                logLevel = LogLevel.Debug;
            case "WARN":
            case "INFO":
                logLevel = LogLevel.Info;
            case "ERROR":
                logLevel = LogLevel.Err;
            case "FATAL":
                logLevel = LogLevel.Critical;
        LogService.Log(LogNameEnum.Exception, LogCategoryEnum.BusinessLogic, logLevel, RenderLoggingEvent(loggingEvent));

And here is an example of how it's used in my code. After instantiating an ILog object, logging is as simple as calling the Error() (or whatever type of log you want to record) method on the object.

public class Service : IHttpHandler
    protected static readonly ILog log = 
    public void ProcessRequest(HttpContext context)
        catch (Exception ex)

Hope this helps!


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


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
SeriousM3-Apr-14 1:22
memberSeriousM3-Apr-14 1:22 
GeneralRe: My vote of 3 Pin
im1dermike17-Apr-14 8:46
memberim1dermike17-Apr-14 8:46 
QuestionI think you should show your custom Appender Pin
Sacha Barber19-Jun-12 5:22
mvaSacha Barber19-Jun-12 5:22 
AnswerRe: I think you should show your custom Appender Pin
im1dermike19-Jun-12 5:29
memberim1dermike19-Jun-12 5:29 
GeneralNice job Pin
Tim Corey19-Jun-12 5:05
professionalTim Corey19-Jun-12 5:05 

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 | Cookies | Terms of Use | Mobile
Web03 | 2.8.190306.1 | Last Updated 19 Jun 2012
Article Copyright 2012 by im1dermike
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid