Pages

Monday, November 14, 2011

Log4NetDatabaseConfiguration Part1

step 1. Create a New Solution  and a Class library project called 'DatabaseLogger'

step 2. Download Log4net from  here and    and after download ,find log4net-1.2.11\bin\net\2.0\release\log4net.dll and refer it to your project

step 3. create a class Called Logger and an Inerface ILogger
using System;
using System.Configuration;
using System.Globalization;
using log4net;
using System.Diagnostics;

namespace DatabaseLogger
{
    public class DbLogger : IDbLogger
    {
        #region Datamembers

        private static ILog log = null;
        private const string Log4netConnectionString = "Log4netConnectionString";

        #endregion

        #region Class Initializer        
        public Logger()
        {
            log = LogManager.GetLogger(typeof(DbLogger));
            log4net.GlobalContext.Properties["host"] = Environment.MachineName;
            log4net.GlobalContext.Properties["severity"] = "Low";
            log4net.GlobalContext.Properties["userName"] = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            log4net.GlobalContext.Properties["applicationName"] = Process.GetCurrentProcess().ProcessName;
            
        }

        #endregion

        #region IDbLogger Members

        public void EnterMethod(string methodName)
        {
            if (log.IsInfoEnabled)
                
                log.Info(string.Format(CultureInfo.InvariantCulture, "Entering Method {0}", methodName));
        }

        public void LeaveMethod(string methodName)
        {
            if (log.IsInfoEnabled)
                log.Info(string.Format(CultureInfo.InvariantCulture, "Leaving Method {0}", methodName));
        }

        public void LogException(Exception exception)
        {
            if (log.IsErrorEnabled)
                log.Error(string.Format(CultureInfo.InvariantCulture, "{0}", exception.Message), exception);
        }

        public void LogError(string message)
        {
            if (log.IsErrorEnabled)
                
                log.Error(string.Format(CultureInfo.InvariantCulture, "{0}", message));
        }

        public void LogWarningMessage(string message)
        {
            if (log.IsWarnEnabled)
                log.Warn(string.Format(CultureInfo.InvariantCulture, "{0}", message));
        }

        public void LogInfoMessage(string message)
        {
            if (log.IsInfoEnabled)
                
                log.Info(string.Format(CultureInfo.InvariantCulture, "{0}", message));
        }

        #endregion 
    }
}


using System;

namespace DatabaseLogger
{
    public interface IDbLogger
    {
        /// 
        /// Writes a log entry when entering the method.
        /// 
        /// 



Name of the method.
void EnterMethod(string methodName);

        /// 
        /// Writes a log entry when leaving the method.
        /// 
        /// 



Name of the method.
void LeaveMethod(string methodName);

        /// 
        /// Logs the exception.
        /// 
        /// 



The exception.
void LogException(Exception exception);

        /// 
        /// Logs the error.
        /// 
        /// 



The message.
void LogError(string message);

        /// 
        /// Logs the warning message.
        /// 
        /// 



The message.
void LogWarningMessage(string message);

        /// 
        /// Logs the info message.
        /// 
        /// 



The message.
void LogInfoMessage(string message);
    }
}
Step 4: Now We Need to add One more Class Call SqlServerAppender for Log4Net Connection string

using System;
using System.Configuration;
using log4net.Appender;

namespace Database
{
    public class SqlServerAppender : AdoNetAppender
    {
        private const string Log4netConnectionString = "Log4netConnectionString";

        public new string ConnectionString 
        {
            get { return base.ConnectionString; }
            set { base.ConnectionString = ConfigurationManager.ConnectionStrings[Log4netConnectionString].ConnectionString; }
        }
    }
}

after that Your Dll is ready for Refernceing for your Project where you want to implement it .
Now You need Some Configuration for Log4net and what database field you want in the table
I put  four custom fileds alogin with the other filed provieded by Log4net.
I seperated my appconfig file and log4net config file for better understatding.
for seprating you need to add a line of code  in AssembblyInfo.cs continue with part2

7 comments:

  1. This doesnt seem to work. Has a lot of typ-o's

    ReplyDelete
  2. thank u for sharing such good and valuable information on DotNet...a good training center for

    online training for Dot Net

    ReplyDelete
  3. It was so nice article and useful to Informatica learners. we also provide Dotnet Course online training our Cubtraining is leader in providing Software Training

    ReplyDelete
  4. At Coepd (Center of Excellence for Professional Development) we assume Object-Oriented Programming concepts and teaches C#.NET, ADO.NET which helps the attendees to build database-driven Web applications and Web Sites successfully. We also guide the attendees to develop web-based enterprise applications using ASP.NET and Visual Studio which comforts in developing the Web Services using .Net framework in Service-oriented Architecture.

    http://www.coepd.com/DotnetTraining.aspx

    ReplyDelete