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 [%thread] %-5level %logger [%property{NDC}] - %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:
- Log4Net dashboard there is a free developer version
- Hacksaw
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
Chris Houston
May 19, 2009 at 5:31 pm
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
ismailmayat
May 19, 2009 at 6:35 pm
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
Nico Lubbers
October 27, 2009 at 9:25 am
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
ismailmayat
October 27, 2009 at 9:57 am
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
Nico Lubbers
October 27, 2009 at 2:31 pm
Hi Ismael,
Acording to this thread it should also be possible to inherit from Umbraco.Global
See this thread:
http://our.umbraco.org/forum/developers/extending-umbraco/4752-Extending-Globalasax
Nico
Nico Lubbers
October 28, 2009 at 8:50 am
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
ismailmayat
October 28, 2009 at 10:47 am