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

# 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?

# 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