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

4 Comments

  • It was a time saver :)

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

  • 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:





















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

    Thanks.

  • 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















    xml Output :

    Logging done at Debug Level.
    Logging done at Info Level.
    Logging done at Warn Level.
    Logging done at Error Level.
    Logging done at Fatal Level.



    Regards,
    Kaushal Patel

Comments have been disabled for this content.