AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Tuesday 2 February 2016

Enable log4net in application.

In this blog article I will discuss how to enable log4net in application. Log4net is used to create a log in application. Please follow below steps to enable log4net.

Step 1- Install log4net from nuget package manager.
  
Step 2- Add following code in your config file.

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <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" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>

Step 3- Create instance of log4net to write logs.

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

Step 4- Now start write logs in your code.

   log.Info("---------This Message will saved in log----------");

Check your directory bin/Debug/  new folder logs is created where the log.txt file is created.

   1 comment :

  • Popular Posts
  • Comments