Monday, April 2, 2012

Log4Net Logging


Log4Net is a third party assembly, using which the logging functionality in an application could be error logging or info logging, etc… can be handled.
Logging the message in a log file or Event logs or Database table or Can be displayed in a console.
Below sample explains the same

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using log4net.Appender;
using log4net.Layout;
using log4net.Repository;
using log4net.Core;
namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //creating a log4net repository
                log4net.Repository.ILoggerRepository objRepository = log4net.LogManager.CreateRepository("MyRep");

                //applying setting to append logs in a file.
                //file name will be found in the application folder path with name same as repository name
                ConfigureFileAppender(objRepository);

                //applying setting to add logs to event viewer
                ConfigureEventLogAppender(objRepository);

                //applying setting to display the message in the console
                ConfigureConsoleAppender(objRepository);
                log4net.ILog objLog = log4net.LogManager.GetLogger("MyRep", typeof(MyInterface));

                //writing the debug message
                objLog.Debug("Test Message");
            }
            catch (Exception exp)
            {
                Console.WriteLine("Error : " + exp.Message);
            }
            Console.Read();
        }

        private static void ConfigureConsoleAppender(ILoggerRepository repository)
        {
            MyConsoleAppender objMyConsoleAppender = new MyConsoleAppender();
            objMyConsoleAppender.ActivateOptions();
            BasicConfigurator.Configure(repository, objMyConsoleAppender);

        }
        private static void ConfigureEventLogAppender(ILoggerRepository repository)
        {
            MyEventLogAppender objMyEventLogAppender = new MyEventLogAppender("MyApp", "MyLog");
            objMyEventLogAppender.Threshold = Level.All;
            BasicConfigurator.Configure(repository, objMyEventLogAppender);
        }

        private static void ConfigureFileAppender(ILoggerRepository repository)
        {
            MyRollingFileAppender objMyRollingFileAppender = new MyRollingFileAppender(repository.Name);
            objMyRollingFileAppender.Threshold = Level.All;
            objMyRollingFileAppender.ActivateOptions();
            BasicConfigurator.Configure(repository, objMyRollingFileAppender);
        }
    }

    sealed class MyRollingFileAppender : RollingFileAppender
    {
        public MyRollingFileAppender(string appName)
        {
            //setting the properties relevant to the log files
            this.Name = "MyLogFile";
            this.File = appName + ".log";
            this.AppendToFile = true;
            this.MaximumFileSize = "10000KB";
            this.MaxSizeRollBackups = 50;
            this.Layout = new PatternLayout(@"%date [%thread] %-5level %logger [%ndc] - %message%newline");
        }
    }
    sealed class MyEventLogAppender : EventLogAppender
    {
        public MyEventLogAppender(string appName, string logName)
        {
            //setting the properties relevant to event logs
            this.Name = "AfpEventLogAppender";
            this.ApplicationName = appName;
            this.MachineName = ".";
            this.LogName = logName;
            this.Layout = new PatternLayout(
                @"%date [%thread] %-5level %logger %method() [%ndc] - %message%newline");
        }
    }
    sealed class MyConsoleAppender : ConsoleAppender
    {
        public MyConsoleAppender()
        {
            //setting the properties relevant to the console
            Name = @"ConsoleAppender";
            Layout = new PatternLayout(
                @"%date [%thread] %-5level %logger %method() [%ndc] - %message%newline");

        }
    }

    public interface MyInterface
    {
        void Test();
    }
    public class Cls : MyInterface
    {
        public void Test()
        {
            throw new NotImplementedException();
        }
    }
}


No comments: