Log4jNet Logging in ASP.NET MVC

What to cover:

1. Install nugget package for Log4Net.

2. Entry of package on Global.asax file

3. Configuration on Web.config file.

4. Define Log4j appenders on Web.config file.

5. Code to initialize log4j on class.

6. Code to print log on methods.

Step 1:

Create ASP.NET MVC project with any name you want. Right click on References

Step 2:

Select Manage NuGet Packages under References.

Step 3:

Select Browse tab and type Log4Net and Install and then click Ok. It will take some time.

Step 4:

After installing check on Installed tab and you will see Log4Net on installed section. Means It installed on your project.

Step 5:

As installing step 4 is complete now go to your project Global.asax file and add below entry on last of the list under Application_Start() method.

log4net.Config.XmlConfigurator.Configure();

Step 6:

Now go to Web.config file which is last filer on project explorer as seen on below snaps and add below entry under <configuration> section.

<log4net debug=”false”>

<appender name=”LogFileAppender” type=”log4net.Appender.FileAppender”>

<param name=”File” value=”<path of your file>/<name of your file>.log” />

<! — <param name=”AppendToFile” value=”true”/> →

<layout type=”log4net.Layout.PatternLayout”>

<param name=”ConversionPattern” value=”%d [%t] %-5p %c %m%n” />

</layout>

</appender>

<root>

<level value=”All” />

<appender-ref ref=”LogFileAppender” />

</root>

</log4net>

Step 7:

Now again in Web.config add entry below entry under <configSections> section which is just after <configuration> from top.

<configSections>

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

</configSections>

Step 8:

Now all configurations have been done. Go to your class where code have been written. In my case below is code snippet.

Code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Http;

using System.Web.Mvc;

namespace Log4NetLogging.Controllers

{

[System.Web.Http.RoutePrefix(“api/Employee”)]

public class EmployeeController : ApiController

{

log4net.ILog logger = log4net.LogManager.GetLogger(typeof(EmployeeController)); //Initialize Log4Net configuration to use log4net method for print logs

[System.Web.Http.Route(“Message”)]

public string GetMessage()

{

// https://gorest.co.in/public/v2/users

logger.Info(“Print log message”); //Method to print log message on file

return “Print log message”;

}

}

}

· To initialize log4net object on class just once on the top of the code just after class name.

log4net.ILog logger = log4net.LogManager.GetLogger(typeof(EmployeeController));

· Call log method from above initialization to print log message on file at every steps of the class you want to print.

logger.Info(“Print log message”);

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adil Abdullah

Adil Abdullah

Currently working as Full stack Java developer for Swedish company Easit AB. Working on Java and IBM stack.