Click here to Skip to main content
14,037,415 members
Click here to Skip to main content
Add your own
alternative version

Stats

85.5K views
24 bookmarked
Posted 25 Sep 2014
Licenced CPOL

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

, 25 Sep 2014
Rate this:
Please Sign up or sign in to vote.
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.

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.

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

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)]

Step 4

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

<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.

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)

Share

About the Author

Mathew Soji
Software Developer
India India
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
QuestionHow about an example of using Log4Net in an Razor / cshtml view page :-) Pin
Terry Clancy30-May-18 19:34
memberTerry Clancy30-May-18 19:34 
QuestionUnity and log4net Pin
STRICQ15-May-16 7:05
memberSTRICQ15-May-16 7:05 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun21-Oct-15 1:54
memberHumayun 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
memberravimittal847-Sep-15 22:31 
GeneralMy vote of 1 Pin
Michael Trembovler30-Jul-15 2:56
memberMichael Trembovler30-Jul-15 2:56 
QuestionMy Pin
Ananth r28-Jul-15 4:31
memberAnanth 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
memberbob.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
memberUmar Adeeb12-Jan-15 9:42 
GeneralMy vote of 4 Pin
Thomas Maierhofer (Tom)25-Sep-14 20:18
memberThomas 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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190424.1 | Last Updated 26 Sep 2014
Article Copyright 2014 by Mathew Soji
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid