Click here to Skip to main content
15,867,308 members
Articles / Web Development / Apache

How to Use Apache log4net Library with ASP.NET MVC 5 for Logging

Rate me:
Please Sign up or sign in to vote.
4.81/5 (30 votes)
25 Sep 2014CPOL1 min read 106.7K   24   11
Logging is a method of tracking/monitoring what is going on when an application is in progress/running.

Introduction

Logging is a method of tracking/monitoring what is going on when an application is in progress/running. Log records will be most needed items when something goes wrong in your application, be it Windows Forms, mobile or web applications.

Here I will be walking through the basic steps in implementing logging functionality using Apache log4net framework in an ASP.NET MVC 5 application.

I am using Visual Studio Express 2013 for Web as my development environment targeting .NET framework 4.5.

Step 1

Open Visual Studio 2013 for Web and create a new ASP.NET Web application selecting MVC template.

Image 1

Step 2

Here in this demo application, we are going to use Apache log4net framework for logging. We need to add reference of log4net DLL using NuGet package manager.

  • In VS 2013 Solution Explorer -> Right click on Reference and Select Manage NuGet Packages.
  • Search for ‘log4net’ and Install.

Image 2

Image 3

Once installation is successful, we can see the log4net DLL added under the Solution explorer Reference section as shown below:

Image 4

Step 3

Next, we need to configure our application to use log4net logging framework. Add the below line in your startup.cs file in ASP.NET MVC5 Solution folder. The below line of code provides information about log4net configuration file.

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]

Image 5

Step 4

Next, add the below section to web.config file.

HTML
<configSections>
  <!-- Add log4net config section-->
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,     log4net" />
</configSections>

<log4net debug="true">
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="logs\log.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    </layout>
  </appender>

  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
</log4net>

Step 5

Next modify Global.asax.cs and add the below code inside Application_Start() method.

log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));

Now our log4net library is ready to use with MVC5 application.

Step 6

Add logger declaration in classes for which we want to make logs as below:

readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Step 7

Use the logger.Error() method to log messages when needed.

Image 6

Run an application and we can see the log file generated under the logs folder under the application root directory as configured in the web config file.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow about an example of using Log4Net in an Razor / cshtml view page :-) Pin
Terry Clancy30-May-18 19:34
Terry Clancy30-May-18 19:34 
QuestionUnity and log4net Pin
STRICQ15-May-16 7:05
STRICQ15-May-16 7:05 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun21-Oct-15 1:54
Humayun Kabir Mamun21-Oct-15 1:54 
Suggestion[My vote of 2] Would have liked to see the explanation and alternatives for each step Pin
ravimittal847-Sep-15 22:31
ravimittal847-Sep-15 22:31 
GeneralMy vote of 1 Pin
Michael Trembovler30-Jul-15 2:56
Michael Trembovler30-Jul-15 2:56 
QuestionMy Pin
Ananth r28-Jul-15 4:31
Ananth r28-Jul-15 4:31 
AnswerRe: My Pin
Mathew Soji28-Jul-15 4:45
professionalMathew Soji28-Jul-15 4:45 
GeneralMy vote of 5 Pin
bob.good23-Jan-15 14:49
bob.good23-Jan-15 14:49 
GeneralRe: My vote of 5 Pin
Mathew Soji23-Jan-15 15:28
professionalMathew Soji23-Jan-15 15:28 
QuestionMy vote of 5 Pin
Umar Adeeb12-Jan-15 9:42
Umar Adeeb12-Jan-15 9:42 
GeneralMy vote of 4 Pin
Thomas Maierhofer (Tom)25-Sep-14 20:18
Thomas Maierhofer (Tom)25-Sep-14 20:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.