How To Implement Log4net Error Logger in c#





Logging Levels

There are seven logging levels, five of which can be called in your code. They are as follows (with the highest being at the top of the list):

  1. OFF - nothing gets logged (cannot be called)
  2. FATAL
  3. ERROR
  4. WARN
  5. INFO
  6. DEBUG
  7. ALL - everything gets logged (cannot be called)

These levels will be used multiple times, both in your code as well as in the config file. There are no set rules on what these levels represent (except the first and last).



Log4Net Tutorial in C# .net (How can I show log in a file?)
For logging service my choice is log4net from Apache Software Foundation. It is easy to use, open source and well documented. There are also so many logging services but they are not open source. So it is an easy and best solution for you.
Write Log in Console procedures are given below-
2. Open visual studio and create a new console application.
3. Add to the project a reference to the \bin\net\2.0\release\log4net.dll assembly in the log4net distribution.
4. write the main method like this

01using System;
02using System.Collections.Generic;
03using System.Text;
04using log4net;
05using log4net.Config;
06  
07namespace LogPractice
08{
09  class Program
10  {
11    void Main(string[] args)
12    {
13     log4net.Config.BasicConfigurator.Configure();
14     log4net.ILog log = log4net.LogManager.GetLogger(Program);
15log.Debug("THis is sadi's world!");
16     log.Info("How beautyful the console looks like");
17     log.Warn("You are great you did this");
18     log.Error("Who make you know is the best");
19     log.Fatal("sadi the great");
20     Console.ReadLine();  // Hold the output
21     }
22   }
23 }
Using Log4net Write log in a file, Procedures are given below-
1. Download log4net from http://logging.apache.org/log4net/download.html
2. Open visual studio and create an application.
3. Add to the project a reference to the \bin\net\2.0\release\log4net.dll assembly in the log4net distribution.
4. Now put this web.config/app.config file in configuration tag.

01<configSections>
02  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
03</configSections>
04<log4net>
05<root>
06 <level value="DEBUG" />
07 <appender-ref ref="LogFileAppender" />
08</root>
09<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
10 <param name="File" value="C:\Try\logger\logger\bin\Debug\log.txt" />
11 <param name="AppendToFile" value="true" />
12 <rollingStyle value="Size" />
13 <maxSizeRollBackups value="10" />
14 <maximumFileSize value="10MB" />
15 <staticLogFileName value="true" />
16 <layout type="log4net.Layout.PatternLayout">
17 <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
18 </layout>
19</appender>
20</log4net>
this configuration creates a log file in C:\temp\log.txt.
5. To use log4net put this as a local class variable:   protected static readonly ILog log =
LogManager.GetLogger(Program);
6.    And do this to write messages in the log file.   log.Debug(“this text will be in log file”);
For Example:

01using System;
02using System.Collections.Generic;
03using System.Text;
04using log4net;
05using log4net.Config;
06  
07namespace <code>LogPractice</code>
08{
09 class Program
10 {
11 protected static readonly ILog log = LogManager.GetLogger(typeof(Program));
12 static void Main(string[] args)
13 {
14  
15 log4net.Config.XmlConfigurator.Configure();
16 //————————–
17 log.Warn("sadi the great");
18 }
19 }
20}
7. Compile and run the application, and you’ll see output to the console
N.B : if you run this code log will show in the c:/temp/log.txt file.

Another One Example  :


Log4net.Xml File Consists Below :

<?
<
<
<
</

<
<
<
<!--
<
<
<
<
<
<
<!--
<
</
</
<
<
<
</
</
</

Error Logger .Cs File Consists Following



using System;using System.Collections.Generic;using System.Linq;using System.Text;using log4net;using log4net.Config;using System.IO;using System.Reflection;namespaceTest{public class ErrorLogger{




{
public static readonly ILog logger = LogManager.GetLogger(typeof(ErrorLogger));String sConfFile = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\log4net.xml";public ErrorLogger()//log4net.Config.XmlConfigurator.Configure(new FileInfo("c:\\app.config"));log4net.Config.

}

{

logger.Debug(sMessage, eException);
}

{

logger.Debug(sMessage.ToString());
}

{

logger.Debug(sMessage.ToString());
}


{
logger.Info(
XmlConfigurator.Configure(new FileInfo(sConfFile));public void dologging(Object sMessage, Exception eException)if (logger.IsDebugEnabled)public void dologging(Object sMessage)if (logger.IsDebugEnabled)public static void dologgings(Object sMessage)if (logger.IsDebugEnabled)public void Logger()"Start Service");// TODO: add code here logger.Info(


}

}
}
"OrgChart stop");Console.WriteLine("Press [Enter] to exit...");Console.ReadLine();


Then I call the Error Logger in main page


ErrorLogger.dologgings("End of :- " + ds.Tables[0].Rows[Indx]["Work"].ToString());
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="log.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>
view source
view source
view source

Comments