Search

Friday, August 2, 2013

Log4Net in ASP.NET MVC

Confuguring Log4Net in ASP.NET MVC 4 Web Application :

1.  Download Log4Net :

http://logging.apache.org/log4net/download.html 


2. Add reference in ASP.Net MVC 4 application :


Solution Explorer > right click on project-name > add references > browse Log4Net.dll


3. Modify Web.config File :


<configuration>

.
.
.
.


 <configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
 </configSections>
 <log4net>
   <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
   <param name="File" value="LOGS\log.txt"/>
   <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
   <appendToFile value="true" />
   <rollingStyle value="Size" />
   <maxSizeRollBackups value="2" />
   <maximumFileSize value="10MB" />
   <staticLogFileName 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>

   <logger name="test">
     <level value="DEBUG"></level>
   </logger>
 </log4net>
.
.
</configuration>

4. Modify AssemblyInfo.cs  File:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]


5. Modify Global.asax.cs File:


protected void Application_Start()

        {
        
      log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
          }


6. Add logger declaration in class for which you want to make logs :


readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);



7. Use Log Functions inside class:

log.error(“make mistakes”);


I hope this information will reduce your search effort and you will get started with your Logger ASAP.
                                                                                                                                     -Cheers.

No comments:

Post a Comment