Logging your application with Log4Net

In order to better understand behaviour of your application and understand how your users are using it, you need to have a logging mechanism in

The Logging Services project is intended to provide cross-language logging services for purposes of application debugging and auditing.

So, why use logging?
Well, first of all, it is a good way to debug your application and to get a hold on what is going on. For example, on production environments, many times you won't be able to debug and, therefore, you won't be able to see what is going with your application.
Here comes logging. By correctly logging, you will be able to identify issues, see what logic is being executed, etc..
Best strategies and how to successfully log applications will be covered in other tutorial.

For all our application in .NET, we are using Log4Net.

In order to set it up correctly, first we need to get that package from Nugget.
So, open up your Nugget package manager in Visual Studio and type in: log4net
As of writing this article, the latest version is 2.0.8
Click install and wait for the package to be installed.

Great! Now, we will need to add some code in order for logging to work.

First, we need to alter Web.config or App.config to include the log4net.
Open your config file and add, in the configSections:

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

And then, we need to add a log4net element to the config also, so, paste this in:

    <appender name="RollingFileAppenderAll" type="log4net.Appender.RollingFileAppender">
      <file value="D:\log\Name.Of.Your.Application.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value=".yyyy-MM-dd" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="20MB" />
      <staticLogFileName value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="FATAL" />
      <level value="DEBUG" />
      <appender-ref ref="RollingFileAppenderAll" />

This is the base configuration, for more, you can read on this link.

Now, we need to wire it up with our Startup or Global.asax class.

using log4net;
using log4net.Config;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

[assembly: XmlConfigurator(Watch = true)]
namespace WebApplication1
    public class WebApiApplication : System.Web.HttpApplication
        private static readonly ILog _log = LogManager.GetLogger(typeof(WebApiApplication));
        protected void Application_Start()
            _log.Debug("Starting Application");

In each class you want to use logging, just add this as global property of the class and use it inside that class as you see fit:

private static readonly ILog _log = LogManager.GetLogger(typeof(WebApiApplication));

If you are looking for Website Development Company that specializes in .NET, please don't hesitate to contact us.

Comments (0)

Leave a comment

Dire Solutions Logo Dark Vertical