Click here to Skip to main content
14,425,525 members

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

Rate this:
4.81 (30 votes)
Please Sign up or sign in to vote.
4.81 (30 votes)
25 Sep 2014CPOL
Logging is a method of tracking/monitoring what is going on when an application is in progress/running.


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.

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

<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" />

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

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.


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


About the Author

Mathew Soji
Software Developer
India India
No Biography provided

Comments and Discussions

QuestionHow about an example of using Log4Net in an Razor / cshtml view page :-) Pin
Terry Clancy30-May-18 20:34
MemberTerry Clancy30-May-18 20:34 
QuestionUnity and log4net Pin
STRICQ15-May-16 8:05
MemberSTRICQ15-May-16 8:05 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun21-Oct-15 2:54
MemberHumayun Kabir Mamun21-Oct-15 2:54 
Suggestion[My vote of 2] Would have liked to see the explanation and alternatives for each step Pin
ravimittal847-Sep-15 23:31
Memberravimittal847-Sep-15 23:31 
GeneralMy vote of 1 Pin
Michael Trembovler30-Jul-15 3:56
MemberMichael Trembovler30-Jul-15 3:56 
QuestionMy Pin
Ananth r28-Jul-15 5:31
MemberAnanth r28-Jul-15 5:31 
AnswerRe: My Pin
Mathew Soji28-Jul-15 5:45
professionalMathew Soji28-Jul-15 5:45 
GeneralMy vote of 5 Pin
bob.good23-Jan-15 15:49
Memberbob.good23-Jan-15 15:49 
GeneralRe: My vote of 5 Pin
Mathew Soji23-Jan-15 16:28
professionalMathew Soji23-Jan-15 16:28 
QuestionMy vote of 5 Pin
Umar Adeeb12-Jan-15 10:42
MemberUmar Adeeb12-Jan-15 10:42 
GeneralMy vote of 4 Pin
Thomas Maierhofer (Tom)25-Sep-14 21:18
MemberThomas Maierhofer (Tom)25-Sep-14 21: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.

Posted 25 Sep 2014


24 bookmarked