ASP.NET MVC 3.0 Hosting :: How to Use Apache’s Log4net Logging Framework

ASP.NET MVC 3.0 Hosting :: How to Use Apache’s Log4net Logging Framework

Today, we will give you a short overview of the steps required to integrate the Apache log4net logging frameork to an ASP.NET MVC 3 Web application.

For easiness, we are beginning from ASP.NET MVC 3 Web application and select the “Internet Application” layout to have some default controllers and views. To insert the log4net one could with the Nuget extension manager:

PM> install-package Log4Net

Alternatively, you can also download and add the log4net.dll reference manually. An easy way to configure the log4net is to add the following tags to your Web.config file:

The sample configuration above includes an appender that logs all sort of messages to the document C:\temp\log4net.log. In a genuine task you ought to utilize a more detailed configuration. For instance, one could configure an appender that sends log messages with level ERROR or FATAL to the system administrator.

The last thing we have to do is to tell log4net to read the XML configuration. One could either edit the Global.asax.cs file and add the following lines:

 or instead of editing the Global.asax.cs file one could add the following line to the AssemblyInfo.cs file:

 To test the log4net logging we add the following lines of code to the HomeController.cs file:

 After launching the project and browsing to the Home page log4net creates the log file C:\Temp\log4net.log and adds the following log message for each request:

Leave a Reply

Your email address will not be published.