Thursday, December 20, 2012

Simplest implementation of log4net


log4net is a powerful logging framework for dotnet. However most people go for there own implementation for logging framework to handle logs. I personally feel it's may be due to lack of simple implementation of log4net.

Simplest implementation of log4net

Step 1: Create a central logging class

public static class Logger
{
       private static log4net.ILog Log { getset; }

       static Logger()
       {
              //log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
              Log = log4net.LogManager.GetLogger(typeof(Logger));
       }

       public static void Error(object msg)
       {
              Log.Error(msg);
       }

       public static void Error(object msg, Exception ex)
       {
              Log.Error(msg, ex);
       }

       public static void Error(Exception ex)
       {
              Log.Error(ex.Message, ex);
       }

       public static void Info(object msg)
       {
              Log.Info(msg);
       }
}
Step 2: Update the a AssemblyInfo.cs file in the Properties folder


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


Step 3: Create a config named log4net.config as specified in the step 2.


xml version="1.0" encoding="utf-8" ?>
<configuration> 
       <configSections>
              <section name="log4net" 
type="log4net.Config.Log4NetConfigurationSectionHandler,
              log4net" />
       </configSections>

       <log4net>
              <appender name="EventLogAppender" 
type="log4net.Appender.EventLogAppender" >
                     <applicationName value="AppName" />
                     <layout type="log4net.Layout.PatternLayout">
                           <conversionPattern 
value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
                     </layout>
              </appender>

              <appender name="LogFileAppender" 
type="log4net.Appender.RollingFileAppender" >
                     <param name="File" value="C:\Temp\Log\log.txt" />
                     <param name="AppendToFile" value="true" />
                     <rollingStyle value="Size" />
                     <maxSizeRollBackups value="2" />
                     <maximumFileSize value="10MB" />
                     <staticLogFileName value="true" />
                     <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
                     <layout type="log4net.Layout.PatternLayout">
                           <param name="ConversionPattern" 
value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
                           <conversionPattern
value="%newline%newline%date [%thread] %newline%-5level %newline%logger [%property{NDC}] %newline>> %message%newline" />
                     </layout>
              </appender>

              <root>
                     <level value="ALL" />    
                     <appender-ref ref="LogFileAppender" />
                     <appender-ref ref="EventLogAppender" />
              </root>
       </log4net> 
</configuration>

In this config file have 2 appenders only. Refer Apache site for more config file samples

Step 4: DONE!!!!!

Step 5: Using it :)
Since all the logging methods exposed by Logger class are a static, we call simply call them to do the logging.
try
{
       return 5/0;
}
catch (Exception ex)
{
       Logger.Error(ex);
}

Happy coding!!!

Sunday, March 22, 2009

Who Am I?

Hi Everybody, First of all I would like to thank you people for look into my blog.

This my first ever attempt to write something that I have learn from my experience.


I am Riju;  a person, who like to code in a perfect possible way...

However as you all know, there is nothing consider as perfect as "Death".

So I decide to be as close as to Death....

I need your feedback to raise into the state of Death..