Integrating log4net into Umbraco site.

Following on from Lee Kelleher’s excellent post on integrating ELMAH into Umbraco I have managed to plug in log4Net.  This technique could also be used to plugin anything else that needs access to events in global.asax for example a dependancy injection library (I will at some point blog about wiring up Ninject into Umbraco).  If you want to use log4Net with Umbraco here is what you need todo:

  • Download log4Net
  • Create a  project with class that inherits from HttpWebApplication class mine looks like:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Log4net4Umbraco
    {
        public class Logger : System.Web.HttpApplication
        {
    
            private static log4net.ILog log = log4net.LogManager.GetLogger(typeof(Logger));
    
            public Logger()
    		{
    			InitializeComponent();
    		}
    
            protected void Application_Start(Object sender, EventArgs e)
            {
                // Application start-up code goes here.
                log4net.Config.XmlConfigurator.Configure();
    
                log.Debug("Fired up logger in application_start");
            }
    
            protected void Application_End(Object sender, EventArgs e)
            {
    
                log.Debug("closed logger in application_end");
                log4net.LogManager.Shutdown();
    
            }
    
     	    protected void Application_Error(object sender, EventArgs e)
     	    {
     	        // Code that runs when an unhandled error occurs
    
     	        log.Error("Unhandled exception", Context.Error);
    
     	    }
    
            private void InitializeComponent()
            {
    
            }
    
        }
    }
    
    
  • Remove App_global.asax.dll from your website bin. This file is part of umbraco distribution, it has events wired up for Umbraco basically writes start up message to umbraco log. If this is not removed then site will not fire up.
  • Add the following lines to the web.config under configsections
      <configSections>
          <section  name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
      </configSections>
      <log4net configSource="config\log4net.config" />
    
  • Under the config directory I added a config file called log4net.config for all the available options see the log4net documentation.
    <?xml version="1.0" encoding="utf-8" ?>
      <log4net>
    
     	    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <threshold value="ALL"/>
     	      <file value="C:\\Inetpub\\mysite\\data\\Logs\\site.log" />
     	      <appendToFile value="true" />
     	      <rollingStyle value="Date" />
     	      <datePattern value="yyyyMMdd" />
     	      <layout type="log4net.Layout.PatternLayout">
     	        <conversionPattern value="%date &#91;%thread&#93; %-5level %logger &#91;%property{NDC}&#93; - %message%newline" />
     	      </layout>
     	    </appender>
    
    		<root>
    				<appender-ref ref="RollingLogFileAppender"/>
    		</root>
    
    </log4net>
    
  • Copy the dll and log4net dlls to the bin of your site
  • Create a new global.asax file if you don’t already have one.  Point the file to the dll you created so in my case I have
    <%@ Application Codebehind="logger.cs" Inherits="Log4net4Umbraco.Logger" %>
    

Now when the application fires up log4net is initialized.  In my usercontrols or xslt extensions all I need to do to start logging is add code like

private static log4net.ILog log = log4net.LogManager.GetLogger(typeof(MyClass));

public void SomeMethod(){
     log.debug("blah blag");
}

Its all well and good logging you need a way to view the logs. So you can use one of the following to view the logs via a dashboard in Umbraco:

Advertisements

10 thoughts on “Integrating log4net into Umbraco site.

  1. Hi Ismail,

    A great post, thanks for sharing. I wonder what sort of extra load this puts onto a server and if it has any impact on the rendering of pages?

    I can immediately think of a site that this would be really useful on right now.

    Have you just tested this on Version 4 or v.3 also?

    Cheers,

    Chris

  2. ismailmayat says:

    Chris,

    From a performance perspective every logging framework will have a hit, however as long as there is nothing crazy going on i.e a log statement after every line of code then there shouldn’t be a problem.

    I have not tried this with v3 and if my memory serves me correctly the global class wires up the timers for publish at and un publish, so you may stop that from working. Might be worth having a peak at the source to see what exactly is going on in v3.

    Regards

    Ismail

  3. Nico Lubbers says:

    Hi Ismail,

    Great solution, but I have a question.
    You say that the dll App_global.asax.dll has to be removed.
    Is it not possible to get it to work without deleting that dll?

    Nico

  4. ismailmayat says:

    Nico,
    if you did not remove then your code will never fire the code in App_global.asax.dll will fire instead, thus you need to remove.

    Regards

    Ismail

  5. Nico Lubbers says:

    We have it working without removing the App_global.asax.dll by creating a HttpModule that handles the init of the log4netmodule

    That way we do not need to modify the umbraco installation

  6. ismailmayat says:

    Nico,

    that won’t work because the Application_start end etc methods cannot be overridden in the derived class. The issue with http handler is it’s fired for every page.

    Regards

    Ismail

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s