How To Add Log4net and Logging into your Umbraco 7 Website

In software development, no matter how hard you try after you launch a website, bugs occur.  In every build, it is essential to have a good strategy to deal with issues.  Umbraco ships with a great logging package called Log4Net but sometimes you need to extend the logging capabilities more than the default Umbraco offering.  In today’s tutorial, I am going to cover  Log4Net, how to install it and how to log errors with it.

Installing Log4Net

If you install Umbraco via Nuget it should install Log4Net.  If you split your code into separate projects, then you may need to manually install Log4Net yourself.  TO install Log4Net, open visual studio, go to options -> NuGet Package Manager.  Make sure you search the online packages and search for Log4Net, like:

umbraco_log4net

Now you have Log4Net installed, we need to configure it to start logging. Open your web.config file and add the following section in the node:

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

Next we need to define a Log4Net logger. In this example we will always write the log file information to a log file on the C dive. The log4net section can go anywhere under the configuration node.

<log4net> 
<appender name="FileAppender" type="log4net.Appender.FileAppender"> 
<file value="C:\logfile.txt" /> 
<appendToFile value="true" /> 
<layout type="log4net.Layout.PatternLayout"> 
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" /> 
</layout> 
</appender> 
<root> 
<level value="DEBUG" /> 
<appender-ref ref="FileAppender" /> 
</root> 
</log4net>

Now we have Log4Net configured, it’s probably worth covering how you can use it to log information. To use Log4Net in your class you need to create an ILog instance. To create an ILog instance you use the LogManager, passing in the current classes type. I’m hoping it’s obvious why we need to pass in the type but just in case… the type declared to help us debug issues later on. When Log4Net logs anything it also logs the class that requested the logging. When the shit hits the fan and you need to start tracing your error log, knowing which classes output which errors makes your life so much simpler.

To create a log instance, we use the code below:

private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

After we have the ILog instance defined we have several options, first we can log an error:

var exception = new Exception();
Logger.Error(exception);
Logger.Error("Error");

When you start using a logger in your solution, it is very easy to log too much information. Bloating your log files can cause you serious issue later on; imagine your websites just went down and you need to figure out what’s gone wrong. You look in the log file and see thousands of entries, how do you find the errors that relate to the outage?

To deal with this problem, Log4Net provides us different logging levels. After you install Log4Net, look in the webroot of your solution in the ‘config’ folder and you should see a file called ‘log4net.config’. In here, as you can probably guess, is all the Log4Net configuration settings.

From the config file we can change the logging level, so in a normal everyday working enviroment, you may want to only log errors. When you’re trying to debug your website then you may only want to display extra information. In Log4Net we can do this using the Log() method.

Logger.Info("Information");

There is also a debug statement:

Logger.Debug("Debug");

In case you are wondering when to use which. I would recommend using ‘info’ when you want to log information about the program and debug for information to try and track down why it isn’t working. There are a few other logging levels but the main thing to remember by using different logging levels you can more easily filter things later on.

If you want to change your logging level, you can do this in either config or code. To do it in code you can use the following snippet:

((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetLoggerRepository()).Root.Level = Level.Debug;
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetLoggerRepository()).RaiseConfigurationChanged(EventArgs.Empy);

When you run your website, if you take a look in ~/App_Data/Logs/ you should find your log files.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

2 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *