C# Logger – A Log4Net Implementation

Logging – one of the most common requirement for almost every application. There are a lot of things we need to log in our applications, like : error, exception, information, warning message etc. There are number of libraries in the market to support logging and Log4Net in one of them which provides a list of decent functionalities expected from a logging library.

I created a very small plug and play wrapper over Log4Net. You just need to install it’s nuget package and you are done.

 

You can find nuget and code details at below links:

Nuget Packages at : Packages Link

Source Code at : CodeNode Repository

 

Code Walk Through :

Logger.cs is the main component of library, which  implement singleton pattern and  expose all required methods for the application logging.

 #region Constructor
 private Logger()
 {
 XmlConfigurator.Configure();
 }
 #endregion

 #region Properties
 /// <summary>
 /// Gets the instance.
 /// </summary>
 /// <value>
 /// The instance.
 /// </value>
 public static Logger Instance
 {
 get { return LoggerInstance.Value; }
 }
 #endregion

As a thumb rule , every singleton class must have private constructor so that it can neither be inherited nor instantiated. So we use private constructor to configure the logger with pre defined configuration which we can find at “Configurations/log4net.config“. This file will be created automatically by installing the nuget package.

Supported methods of Logger.cs are :

#region Public Methods</pre>
/// <summary>
/// Log a message object with the log4net.Core.Level.Debug level.
/// </summary>
/// <param name="message">The message.</param>
public void Debug(object message)
{
LoggerObject.Debug(message);
}

/// <summary>
/// Logs a message object with the log4net.Core.Level.Info level.
/// </summary>
/// <param name="message">The message.</param>
public void Info(object message)
{
LoggerObject.Info(message);
}

/// <summary>
/// Logs a message object with the log4net.Core.Level.Info Warning.
/// </summary>
/// <param name="message">The message.</param>
public void Warning(object message)
{
LoggerObject.Warn(message);
}

/// <summary>
/// Logs a message object with the log4net.Core.Level.Error level.
/// </summary>
/// <param name="message">The message.</param>
public void Error(string message)
{
LoggerObject.Error(message);
}

/// <summary>
/// Log a exception with the log4net.Core.Level.Fatal level.
/// </summary>
/// <param name="ex">The ex.</param>
public void Error(Exception ex)
{
LoggerObject.Error(GeDetailFromExceptipon(ex, ExceptionName));
}

/// <summary>
/// Log a message object with the log4net.Core.Level.Error level including the
/// </summary>
/// <param name="message">The message.</param>
/// <param name="exception">The exception.</param>
public void Error(object message, Exception exception)
{
LoggerObject.Error(message, exception);
}

/// <summary>
/// Log a message object with the log4net.Core.Level.Fatal level.
/// </summary>
/// <param name="message">The message.</param>
public void Fatal(string message)
{
LoggerObject.Fatal(message);
}

/// <summary>
/// Log a exception with the log4net.Core.Level.Fatal level.
/// </summary>
/// <param name="ex">The ex.</param>
public void Fatal(Exception ex)
{
LoggerObject.Fatal(GeDetailFromExceptipon(ex, ExceptionName));
}

/// <summary>
/// Log a message object with the log4net.Core.Level.Fatal level including the
// stack trace of the System.Exception passed as a parameter.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="exception">The exception.</param>
public void Fatal(object message, Exception exception)
{
LoggerObject.Fatal(message, exception);
}
#endregion

So now we can access all logging methods like :

  • Logger.Instance.Debug(“My Debug Message.”);
  • Logger.Instance.Info(“My Info Message.”);
  • Logger.Instance.Warning(“My Warning Message.”);
  • Logger.Instance.Error(ex);
  • Logger.Instance.Fatal(ex);

Configuration Understanding :

After installing nuget package, just confirm two things:

  1. Web.Config should be updated with below settings. This section let log4net know to use “Configurations\log4net.config” as default configuration file.
 <configSections>
 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
 </configSections>
  <log4net configSource="Configurations\log4net.config"/>

2. There should be a Configurations folder at root containing  log4net.config file. This log4net.config has configuration for two file appenders as default.You can add new appenders and change their log  levels as per your requirements.

  • First to log error and fatal level.
    <filter type="log4net.Filter.LevelRangeFilter">
     <levelMin value="ERROR" />
     <levelMax value="FATAL" />
     </filter>
    
  • Second to log information messages
     <filter type="log4net.Filter.LevelRangeFilter">
     <levelMin value="DEBUG" />
     <levelMax value="WARN" />
     </filter>
    

API will create a Logs folder at the application root and will contain an error file like : App.2015-05-20.0.log and a info file like: App.Info.2015-05-20.0.log. Every day it will create new file and appends date to the file name.
Do share the wisdom and motivate us to keep writing such online tutorials for free and do comment if anything is missing or wrong or you need any kind of help.

Keep Learning.. Happy Coding.. :)