Loading...

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:

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

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

<log4net>
    <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" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="FATAL" />
      </filter>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingFileAppenderAll" />
    </root>
</log4net>

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()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            _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 (3)

  • August 24, 2019 12:08  

    http://mewkid.net/order-amoxicillin/ - Amoxicillin 500mg Capsules <a href="http://mewkid.net/order-amoxicillin/">Online Amoxicillin</a> wkw.luls.dire-solutions.com.euu.lp http://mewkid.net/order-amoxicillin/

  • August 24, 2019 12:08  

    http://mewkid.net/order-amoxicillin/ - Buy Amoxicillin <a href="http://mewkid.net/order-amoxicillin/">Amoxicillin 500mg Capsules</a> qca.jidi.dire-solutions.com.sga.kc http://mewkid.net/order-amoxicillin/

  • August 24, 2019 12:08  

    http://mewkid.net/order-amoxicillin/ - Buy Amoxicillin <a href="http://mewkid.net/order-amoxicillin/">Amoxicillin 500mg</a> zud.gago.dire-solutions.com.wps.rj http://mewkid.net/order-amoxicillin/

Leave a comment

Dire Solutions Logo Dark Vertical