A realistic log4net config

Most log4net config file examples show the simplest case.  Here is a more realistic example of a production log4net config that uses multiple appenders.  This comes from an ASP.NET application, but the same technique will work in a server or client application.  This config sets up two appenders:

  1. The first one writes all messages at DEBUG or higher to a log file.  Depending on your needs, a RollingFileAppender that creates a new file every day or week might be more appropriate.
  2. The second appender sends email messages when a new user account is created, or when an error is logged.

<?xml version="1.0"?>

<configuration>

<configSections>

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

</configSections>

<log4net>

<!-- The DebugFileAppender writes all messages to a log file-->

<appender name="DebugFileAppender" type="log4net.Appender.FileAppender">

<file value="LinkedCellsWebService.log" />

<threshold value="DEBUG" />

<appendToFile value="true" />

<layout type="log4net.Layout.PatternLayout">

<param name="ConversionPattern" value="%5p [%d] - %m%n" />

</layout>

</appender>

<!-- The EmailAppender sends an email when something matches the filters-->

<appender name="EmailAppender" type="log4net.Appender.SmtpAppender">

<evaluator type="log4net.Core.LevelEvaluator">

<threshold value="DEBUG"/>

</evaluator>

<!-- The filters are processed in order:

1) match the Inserted New User message

2) match any WARN or higher messages

3) reject everything else -->

<filter type="log4net.Filter.StringMatchFilter">

<stringToMatch value="Inserted a new user" />

<acceptOnMatch value="true" />

</filter>

<filter type="log4net.Filter.LevelRangeFilter">

<levelMin value="WARN" />

<acceptOnMatch value="true" />

</filter>

<filter type="log4net.Filter.DenyAllFilter" />

<!-- The SmtpAppender authenticates against the mail server, the buffersize of 10 provides 10 lines

of context when an error happens. -->

<subject value="LinkedCells: Production log event" />

<to value="notifications@LinkedCells.com" />

<from value="notifications@LinkedCells.com" />

<password value ="password" />

<smtpHost value="MAILSERVER" />

<bufferSize value="10" />

<lossy value="true" />

<layout type="log4net.Layout.PatternLayout">

<param name="ConversionPattern" value="%5p [%d] - %m%n" />

</layout>

</appender>

<root>

<!-- add other appenders here and the log messages will be sent to every listed appender -->

<appender-ref ref="DebugFileAppender" />

<appender-ref ref="EmailAppender" />

</root>

</log4net>

----- END OF CONFIG -----
Published 15 March 2007 12:34 PM by Ted_Graham

Comments

# Ted Graham on .NET : A realistic log4net config said on 15 March, 2007 01:23 PM

PingBack from http://weblogs.asp.net/tgraham/archive/2007/03/15/a-realistic-log4net-config.aspx

# map said on 29 November, 2007 12:38 AM

map http://www.pumixon.0pi.com home link here

# Abdeali said on 23 April, 2008 02:27 AM

It was a time saver :)

# filtering what to log said on 10 June, 2008 02:18 PM

is it possible to set up appenders so that one appender would log messages from one source and another from another?

# ted graham said on 01 July, 2008 08:24 AM

Pingback from  ted graham

# Syed Jazbi said on 25 September, 2008 04:42 PM

My email is sajazbi@hotmail.com.

I am not able to log in XML file. The following message displayed when I open the XML file:

Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.

--------------------------------------------------------------------------------

The operation completed successfully. Error processing resource 'file:///C:/AppLog3.xml'. Line 1, Position 363

Following is the code in my App.Config file:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

 <configSections>

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

 </configSections>

 <log4net debug = "false">

   <appender name="XmlFile" type="log4net.Appender.FileAppender">

     <file value="C:\AppLog3.xml"/>

     <appendToFile value="true"/>

     <layout type="log4net.Layout.XmlLayout">

       <param name="Prefix" value="log4net" />

     </layout>

   </appender>

   <root>

     <level value="WARN"/>

     <appender-ref ref="XmlFile"/>

   </root>

 </log4net>

</configuration>

I don't know what I am doing wrong, I would really appreciate it if you can help me to resolve this issue.

Thanks.

# Kaushal Patel said on 22 October, 2008 03:26 AM

Hi Syed,

I think, you don't have admin rights to create xml file on C: drive.

I have used same config setting which you have used & its working fine.

SimpleLogin.aspx.cs:

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using log4net;

using log4net.Config;

using log4net.Layout;

using log4net.Appender;

public partial class SampleLogging : System.Web.UI.Page

{

   //Creating Object of ILog

   private static readonly ILog Logger = LogManager.GetLogger(typeof(SampleLogging));

   public SampleLogging()

   {

       //BasicConfigurator.Configure();

       XmlConfigurator.Configure();

   }

   protected void Page_Load(object sender, EventArgs e)

   {

       Logger.Debug("Logging done at Debug Level.");

       Logger.Info("Logging done at Info Level.");

       Logger.Warn("Logging done at Warn Level.");

       Logger.Error("Logging done at Error Level.");

       Logger.Fatal("Logging done at Fatal Level.");

   }

}

Web.Config

<root>

     <level value="DEBUG"/>

<appender-ref ref="XmlFileAppender" />

</root>

<appender name="XmlFileAppender" type="log4net.Appender.FileAppender">

     <param name="File" value="C:\XMLLogTest1.xml"/>

     <param name="AppendToFile" value="true"/>

     <layout type="log4net.Layout.XmlLayout">

       <param name="Prefix" value="log4net" />                

     </layout>

</appender>

xml Output :

<log4net:event logger="SampleLogging" timestamp="2008-10-22T12:37:02.4357529+05:30" level="DEBUG" thread="4" domain="72f89c2d-1-128691328188888098" identity="LEH\8kaupate" username="LEH\8kaupate"><log4net:message>Logging done at Debug Level.</log4net:message><log4net:properties><log4net:data name="log4net:HostName" value="mbnbw000053" /></log4net:properties></log4net:event>

<log4net:event logger="SampleLogging" timestamp="2008-10-22T12:37:02.6076312+05:30" level="INFO" thread="4" domain="72f89c2d-1-128691328188888098" identity="LEH\8kaupate" username="LEH\8kaupate"><log4net:message>Logging done at Info Level.</log4net:message><log4net:properties><log4net:data name="log4net:HostName" value="mbnbw000053" /></log4net:properties></log4net:event>

<log4net:event logger="SampleLogging" timestamp="2008-10-22T12:37:02.6232565+05:30" level="WARN" thread="4" domain="72f89c2d-1-128691328188888098" identity="LEH\8kaupate" username="LEH\8kaupate"><log4net:message>Logging done at Warn Level.</log4net:message><log4net:properties><log4net:data name="log4net:HostName" value="mbnbw000053" /></log4net:properties></log4net:event>

<log4net:event logger="SampleLogging" timestamp="2008-10-22T12:37:02.6232565+05:30" level="ERROR" thread="4" domain="72f89c2d-1-128691328188888098" identity="LEH\8kaupate" username="LEH\8kaupate"><log4net:message>Logging done at Error Level.</log4net:message><log4net:properties><log4net:data name="log4net:HostName" value="mbnbw000053" /></log4net:properties></log4net:event>

<log4net:event logger="SampleLogging" timestamp="2008-10-22T12:37:02.6232565+05:30" level="FATAL" thread="4" domain="72f89c2d-1-128691328188888098" identity="LEH\8kaupate" username="LEH\8kaupate"><log4net:message>Logging done at Fatal Level.</log4net:message><log4net:properties><log4net:data name="log4net:HostName" value="mbnbw000053" /></log4net:properties></log4net:event>

Regards,

Kaushal Patel

# Meny said on 02 August, 2010 02:56 PM

Exactly what I needed. Simple and elegant.

# David Sutherland said on 27 September, 2010 01:39 AM

That was great work Ted.  If you have a Starbucks card I'll charge it up so you can get a few coffees.

# Davidoff Cigar said on 15 December, 2010 06:16 AM

Great writing! You might want to follow up to this topic!?

<a href="http://davidoff.corecommerce.com">Davidoff</a>

# Loraine Arellano said on 25 December, 2010 01:19 PM

I need to know just what  has to say about that..

<a href="www.squidoo.com/quesadilla-recipe">Quesadilla Recipe</a>

# Celia said on 26 December, 2010 07:29 AM

I have to hear exactly what  says with this?!

<a href="www.cigars-now.com/.../a>

# Graham Bartlett said on 28 December, 2010 08:08 AM

I wonder just what  says about this!?!

<a href="alternativemedicine.org.in/aromatherapy.html">aromatherapy institute</a>

# Jessica said on 29 December, 2010 02:29 PM

Great post! I want to see a follow up on this topic =D

Thank You

<a href="www.cigars-now.com/.../a>

# Jewell Hays said on 31 December, 2010 05:06 AM

Great writing! I wish you could follow up on this topic???

<a href="www.cigars-now.com/.../a>

# Wilma Mooney said on 01 January, 2011 05:32 AM

Great post! I want to see a follow up on this topic!!!

<a href="www.cigars-now.com/.../a>

# Mara Piper said on 02 January, 2011 03:36 AM

I'm very happy you took the time and wrote this post??

Aaron

<a href="www.iconiccigars.com/.../Davidoff-Classic-No-2-Tubos-Bx-20.html">Davidoff Classic No 2 Tubos</a>

# Marlene Gamble said on 09 January, 2011 09:16 AM

Great post, I have been after something like that!

Gena

<a href="fickmaschine-live.com/">live fickmaschine</a>

# Randolph said on 12 January, 2011 11:19 AM

I'm glad you said that!

<a href="www.gbbilder4you.com/">gaestebuch bilder net categories</a>

# Dorthy said on 14 January, 2011 11:39 AM

The most interesting page I read this month =D

Blaine

<a href="http://www.findgroomers.com">dog grooming</a>

# Rodrick Case said on 15 January, 2011 01:22 PM

Hey Brad, ROFL!

<a href="www.cigars-now.com/.../montecristo.html">monte cristo cigars</a>

# Lara Dodd said on 20 January, 2011 04:55 PM

Maybe the GREATEST post that I have read this year?!?

Sincerest regards,

Colette

<a href="http://www.cigars-now.com">cigars</a>

# Patrice Milligan said on 21 January, 2011 02:41 PM

Hugo, ROFL??

Jennifer

<a href="http://www.7minutemusclereview.net">7 minute muscle</a>

# Rosalinda said on 23 January, 2011 02:51 PM

Howard, ROFL?

<a href="www.cigars-now.com/.../arturo-fuente.html">fuente|a fuente|arturo fuente|fuente cigar|fuente cigars|arturo fuente cigar|arturo fuente cigars}</a>

# Aubrey said on 27 January, 2011 10:50 AM

Wilburn is the best?!

<a href="www.oregonlngpropertysearch.com/">moncler jackets</a>

# Stella Oneal said on 30 January, 2011 12:54 PM

Great read! You may want to follow up on this topic!?!

<a href="www.parketarstvo-cerkvenik.si/.../a>

# Winston Shapiro said on 31 January, 2011 12:33 PM

I have to hear exactly what Tony thinks with that!!

Rosario

<a href="mizarstvo-jereb.si/.../">previjalna miza</a>

# Estella said on 03 February, 2011 04:35 PM

This is the most interesting topic that I have read today..

Rose

<a href="www.netezines.net/claripro-review">claripro review</a>

# Esteban Lamb said on 04 February, 2011 01:52 PM

Great read! You should definitely follow up on this topic :)

Ofelia

<a href="mizarstvo-jereb.si/.../">previjalna miza</a>

# Eugene said on 05 February, 2011 12:35 PM

Great writing! Maybe you could do a follow up on this topic.

Rosa

<a href="http://www.curefortinnitusreview.com">cure for tinnitus review</a>

# Chad said on 09 February, 2011 10:28 AM

I am very happy you took the time and wrote this??

-Sincerest regards,

Lori

<a href="www.butikonlinemurah.com/">Butik Online Murah</a>

# Freddy Cardona said on 11 February, 2011 12:38 PM

I am wondering exactly what Giovanni will say about this!?!

Yours

Chadwick

<a href="www.oregonlngpropertysearch.com/">moncler jackets</a>

# Garrett Gillis said on 13 February, 2011 11:48 AM

I'm very glad you said that post!!

<a href="www.butikonlinemurah.com/">Butik Online Murah</a>

# Jordan Crocker said on 14 February, 2011 02:36 PM

Great blog post, I have been after that :P

-Thanks

Carlene

<a href="www.butikonlinemurah.com/">Butik Online Murah</a>

# Erick said on 16 February, 2011 02:58 PM

Great post! You may want to follow up to this topic!!!

Aline

<a href="www.butikonlinemurah.com/">Butik Online Murah</a>

# Arlene said on 18 February, 2011 04:54 PM

Elmer, lol!!!

<a href="http://www.gume-oblak.si">gume</a>

# Marlene Cohen said on 20 February, 2011 01:39 PM

Jessica rocks =D

-Sincerely

Salvatore

<a href="www.hidenseek.me/.../">2gb netbook</a>

# rtyecript said on 25 August, 2011 12:12 PM

I really liked the article, and the very cool blog

Leave a Comment

(required) 
(required) 
(optional)
(required)