log4net ASP NET MVC Example

Kailash Chandra Behera | Monday, July 27, 2020

Introduction

This blog provides complete guidelines for use of log4net in mvc with log4net rollingfileappender.

Getting Started

Here in the below demonstration, provides step by step guidelines to implement of log4net in MVC application and will create a logger file for storing the log messages from C# ASP.NET MVC.The logger file can be used further to trace logging, it is alternate to event log manager and windows log manager.

Demonstration

  1. Open your MVC project where you want to implement the log4net.

  2. Go to Solution Explorer, then right click on the project name->open Manage NuGet Package for Solution by clicking on the Manage NuGet Package...

  3. Click on the Brose tab, then search for log4net and Install the latest version.

  4. Go to Solution Explorer, then expand the Properties. You will find it just below your project name.

  5. Double click on the AssemblyInfo.cs to open it and paste the below code just after the AssemblyCulture code.

     [assembly: log4net.Config.XmlConfigurator]  
    
  6. Your code placement should look like below code.

     [assembly: AssemblyCulture("")]  
     [assembly: log4net.Config.XmlConfigurator]  
    
  7. Open the web.config file of your MVC project, then find the <configSections></configSections> tag, if you don't find added the <configSections> tag, then copy the below code and past it just after the </configuration> tag.

      <configSections>  
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />  
      </configSections>  
    
  8. If the config Sections tag already there, then copy the below code and paste it just after the <configSections> tag.

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    
  9. Find the </configuration> tag and paste below code just before the </configuration> tag. This is the log level and appender setting in config for log4net, The code should place between <configuration> and </configuration> tag.

     <log4net>  
       <appender name="TestAppender" type="log4net.Appender.RollingFileAppender">  
        <file value="D:\Scanid\Mlogs.log" />  
        <encoding value="utf-8" />  
        <appendToFile value="true" />  
        <rollingStyle value="Date" />  
        <layout type="log4net.Layout.PatternLayout">  
         <conversionPattern value="%date- %message%n" />  
        </layout>  
       </appender>  
       <root>  
        <level value="All" />  
        <!-- If the following line is not included the log file will not be created even if log4net is configured with this file. -->  
        <appender-ref ref="TestAppender" />  
       </root>  
      </log4net>  
    

    log4net RollingFileAppender

  10. Note that here in the log4net config, I have used the log4net RollingFileAppender. to use any other appender or more details regarding appenders, visit to my previous blog.

  11. Your log4net config file should look like below config file

     <?xml version="1.0" encoding="utf-8"?>  
     <!--  
      For more information on how to configure your ASP.NET application, please visit  
      https://go.microsoft.com/fwlink/?LinkId=301880  
      -->  
     <configuration>  
     <configSections>   
       <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />   
      </configSections>   
     <log4net>   
       <appender name="TestAppender" type="log4net.Appender.RollingFileAppender">   
       <file value="D:\Scanid\Mlogs.log" />   
       <encoding value="utf-8" />   
       <appendToFile value="true" />   
       <rollingStyle value="Date" />   
       <layout type="log4net.Layout.PatternLayout">   
        <conversionPattern value="%date- %message%n" />   
       </layout>   
       </appender>   
       <root>   
       <level value="All" />   
       <!-- If the following line is not included the log file will not be created even if log4net is configured with this file. -->   
       <appender-ref ref="TestAppender" />   
       </root>   
      </log4net>   
     </configuration>  
    

    log4net Configuration C#

  12. Add below code one of your controller class to implement log4net and replace the 'Program' with your name of your controller class.

      private static log4net.ILog Log=log4net.LogManager.GetLogger(typeof(Program));  
    
  13. Invoke the methods of logger class to log messages for your project. For example, to write the exception messages invoke the Error function in the catch block.

  14. Now you are done with the implementation of logging, build your project. see the below code for more..

 public class HomeController : Controller  
   {  
     private static log4net.ILog Log = log4net.LogManager.GetLogger(typeof(MainWindow));  
      
     public ActionResult Index()  
     {  
       Log.Info("Window_Loaded event execution started");  
       try  
       {  
         Log.Info("WPF Hello World!");  
         Log.Info("try block executed");  
       }  
       catch (Exception ex)  
       {  
         Log.Error(ex);  
       }  
       finally  
       {  
         Log.Info("Finally block executed");  
       }  
       Log.Info("Window_Loaded event execution completed");  
     }  
   }  

log4net example

Summary

Here in the above of this blog, we discussed the each steps of log4net asp net mvc example. I hope you have enjoued it a lot.

Thanks