How To Use Log4Net In MVC to Track Error

LOG4NET
 
Before showing what Log4Net is I would like to  explaing what logging actually is. 

Logging

In computing, a logfile is a file that records either events that occur in an operating system or in a programme or when a software runs.

Logging is the act of keeping a log.

Or we can say, logging is a method of tracking/monitoring what is going on when an application is running. Log records will be the most needed items when something goes wrong in your application.

Now I am explaing Log4Net.

LOG4NET

Log4net is an open-source library that allows or helps the .NET applications to trace the details of the errors that have occurred in a project.

Now I will explain how we should use Log4Net in our MVC project to track errors.
 
Step 1
 
Create a new MVC project.
 
Step 2
 
Go to the tools and do the following changes as in the picture shown.
 
 
 
Step 3
 
Click on the Manage NuGet Package Manager and then search for Log4Net.
 
 
 
Step 4
 
Install Log4Net.
 
 
 
Step 5
 
 
 
Step 6
 
Now expand the assembly and you will see the  Log4Net.
 
 
 
As we have successfully imported Log4Net, we will now use it.
 
So create a Home controller with the following Action Method.
  1. public class HomeController : Controller  
  2. {  
  3.    log4net.ILog logger = log4net.LogManager.GetLogger(typeof(HomeController));  //Declaring Log4Net  
  4.    public ActionResult Index()  
  5.    {  
  6.       try  
  7.       {  
  8.          int x, y, z;  
  9.          x = 5; y = 0;  
  10.          z = x / y;  
  11.       }  
  12.       catch(Exception ex)  
  13.       {  
  14.          logger.Error(ex.ToString());  
  15.       }  
  16.       return View();  
  17.    }     
  18. }  
Since this method has an error  because I am trying to divide it by zero, it will raise an error. So I will track the error.
 
For this we need to do some setting in web.config. Go to Web.config and paste in the following things.
 
Under <configurations> in web.config write the following code.
  1. <configSections>  
  2. <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />  
  3. </configSections>  
  4.    
  5. <log4net debug="false">  
  6. <appender name="LogFileAppender" type="log4net.Appender.FileAppender">  
  7. <param name="File" value="C:\test\test.log" />  
  8. <!--<param name="AppendToFile" value="true"/>-->  
  9. <layout type="log4net.Layout.PatternLayout">  
  10. <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />  
  11. </layout>  
  12. </appender>  
  13.    
  14. <root>  
  15. <level value="All" />  
  16. <appender-ref ref="LogFileAppender" />  
  17. </root>  
  18. </log4net>  
I am here using the image for a better understanding.
 
 
 
Now  I am explaining on the key concepts.
 
APPENDER: The <appender /> element specifies the name and logger type. It specifies where the information will be logged, how it will be logged and under what circumstances the information will be logged. You can check the various types of appender in the link http://logging.apache.org/log4net/release/config-examples.html.
param name:  specifies the file name and path where the log will be saved. In this case it is "test.log".
 
layout: The Layout element tells Log4Net how each log line should look like. 
Root: You need to have one root section to house your top-level logger references. These are the loggers that inherit information from your base logger (root).
 
Now run the project with the Index action method and Home controller and check the log file in C\Test\Test.log. You will get the details of the  exception.
 
 
 
Since I run it multiple times it is showing the same error multiple times with the date and time. So in this way we can use this Log4Net to track errors in a realistic scenario.


Similar Articles