using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using log4net.Config;
namespace UEUserGuide
{
///
/// This class provide the logging/tracing functionalities.
///
public sealed class Logger
{
///////////////////////////////////////////////////////////////////////
#region Internal fields
///
/// Logger component reference (log4net).
///
private ILog _logger;
#endregion Internal fields
///////////////////////////////////////////////////////////////////////
#region Thread-safe Singleton implementation
///
/// Unique instance of .
///
private static volatile Logger instance = null;
///
/// Synchronisation object.
///
private static object _syncRoot = new Object();
///
/// Internal ctor.
///
private Logger()
{
}
///
/// Instance accessor.
///
///
/// The code below shows how to do Logger access:
///
/// public GenericSAFrm()
/// {
/// // Initialize logger.
/// Logger.Instance.Initialize("GenericSA");
///
/// InitializeComponent();
/// }
///
///
public static Logger Instance
{
get
{
lock (_syncRoot)
{
if (instance == null)
{
instance = new Logger();
}
return instance;
}
}
}
#endregion Thread-safe Singleton implementation
///////////////////////////////////////////////////////////////////////
#region Logger methods.
///
/// Gets the Debug level status: true if set, false otherwise.
///
public bool IsDebugLevelSet { get { return _logger.IsDebugEnabled; } }
///
/// Gets the Info level status: true if set, false otherwise.
///
public bool IsInfoLevelSet { get { return _logger.IsInfoEnabled; } }
///
/// Gets the Warning level status: true if set, false otherwise.
///
public bool IsWarningLevelSet { get { return _logger.IsWarnEnabled; } }
///
/// Initialize the logger component.
///
/// Logger name - see application configuation file, log4net XML element.
///
/// The code below shows how to do Logger access:
///
/// public GenericSAFrm()
/// {
/// // Initialize logger.
/// Logger.Instance.Initialize("GenericSA");
///
/// InitializeComponent();
/// }
///
///
public void Initialize(string name)
{
if (string.IsNullOrEmpty(name))
{
_logger = LogManager.GetLogger("TraceAppender");
XmlConfigurator.Configure();
}
else
{
_logger = LogManager.GetLogger(name);
XmlConfigurator.Configure();
}
}
///
/// Log information message.
///
/// Message format - IFormater.
/// List of argument to pass to the formater.
///
/// The code below shows how to do Logger access:
///
/// if (Logger.Instance.IsInfoLevelSet) Logger.Instance.InfoLogger("GenericSAFrm.Window_Loaded: All is ok, continue!");
///
///
public void InfoLogger(string fmt, params object[] args)
{
if (_logger.IsInfoEnabled) _logger.InfoFormat(fmt, args);
}
///
/// Log warning message.
///
/// Message format - IFormater.
/// List of argument to pass to the formater.
///
/// The code below shows how to do Logger access:
///
/// Logger.Instance.WarningLogger("GenericSAFrm.Window_Loaded: Configuration mismatch!");
///
///
public void WarningLogger(string fmt, params object[] args)
{
if (_logger.IsWarnEnabled) _logger.WarnFormat(fmt, args);
}
///
/// Log error message.
///
/// Message format - IFormater.
/// List of argument to pass to the formater.
///
/// The code below shows how to do Logger access:
///
/// Logger.Instance.ErrorLogger("GenericSAFrm.Window_Loaded: Wrong parameters.");
///
///
public void ErrorLogger(string fmt, params object[] args)
{
if (_logger.IsErrorEnabled) _logger.ErrorFormat(fmt, args);
}
///
/// Log debug message.
/// Visible only if level in root XML element is set to ALL or DEBUG.
///
/// Message format - IFormater.
/// List of argument to pass to the formater.
///
/// The code below shows how to do Logger access:
///
/// if (Logger.Instance.IsDebugLevelSet) Logger.Instance.DebugLogger("GenericSAFrm.Window_Loaded: Parameters {0} is validated.", counter);
///
///
public void DebugLogger(string fmt, params object[] args)
{
if (_logger.IsDebugEnabled) _logger.DebugFormat(fmt, args);
}
#endregion Logger methods.
///////////////////////////////////////////////////////////////////////
#region Internal method helpers.
#endregion Internal method helpers.
}
}