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.
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:
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.
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:
After we have the ILog instance defined we have several options, first we can log an 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.
There is also a debug statement:
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:
When you run your website, if you take a look in ~/App_Data/Logs/ you should find your log files.