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:

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:

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.

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:

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

MY EPISERVER BOOK

EPISERVER NEWSLETTER

Connect

Hi, I'm Jon, I write articles about creating and optimizing websites as well as making money from your online business. I am technical architect and technology fanatic by profession. You can find out more about me by signing up to my newsletter.