How to Implement Error Logger in c# chapter 2

Error Logger.CS Consists

  


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using System.IO;
using System.Reflection;
namespace FireflyCacheClean
{
   public class ErrorLogger
    {
       public  static readonly ILog logger = LogManager.GetLogger(typeof(ErrorLogger));
       String sConfFile = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "file://log4net.xml/";
    
       public ErrorLogger()
       {
     //log4net.Config.XmlConfigurator.Configure(new FileInfo("c:\\app.config"));
           log4net.Config.XmlConfigurator.Configure(new FileInfo(sConfFile));
  
       }
       public void dologging(Object sMessage, Exception eException)
       {
           if (logger.IsDebugEnabled)
               logger.Debug(sMessage, eException);
       }
       public void dologging(Object sMessage)
       {
           if (logger.IsDebugEnabled)
               logger.Debug(sMessage.ToString());
       }
       public static void dologgings(Object sMessage)
       {
           if (logger.IsDebugEnabled)
               logger.Debug(sMessage.ToString());
       }

       public void Logger()
       {
           logger.Info("Start Service");
           // TODO: add code here            
           logger.Info("OrgChart stop");
           Console.WriteLine("Press [Enter] to exit...");
           Console.ReadLine();
       }

    }
}


Log4net.xml Consists


<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
 
  <log4net debug="true" threshold="ON">
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Precachelog.txt" />
      <!--<file value="${APPDATA}\MyApp\MyApp Client\logs\Log.txt"/>-->
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <!--<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />-->
        <param name="ConversionPattern" value="%-5p%d{dd-MM-yyyy hh:mm:ss} – %m%n" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG"  />
      <appender-ref ref="LogFileAppender" />
    </root>
  </log4net>
</configuration>


using whereever we want


                                 ErrorLogger.dologgings("End of Deleting Data for the  batch:-  " + ds.Tables[0].Rows[Indx]["WorksetName"].ToString());

Comments