//----------------------------------------------------------------------- // // Copyright (c) 2012 Deltares. All rights reserved. // // B. Faassen // barry.faassen@deltares.nl // 29-7-2012 // See class header //----------------------------------------------------------------------- using System; using System.Collections.Generic; using log4net; using log4net.Appender; using log4net.Repository; namespace Deltares.Standard.Application { /// /// LogHelper is a utility class to simplify logging to different appenders. /// This class uses log4net to delegate the actual logging. See the log4net /// documentation to configure this API in the client application using either /// the configuration file or programmaticaly. /// /// TODO: This logger should be able to log to EntLib too! /// see http://stackoverflow.com/questions/710863/log4net-vs-nlog /// public class LogHelper { private readonly object sync = new object(); private readonly List exceptions = new List(); /// /// Creates a LogHelper class to simplify logging /// /// The target object to log for. This object type name will be used in the log /// An instance of this helper public static LogHelper Create(object obj) { if (obj == null) throw new ArgumentNullException("obj"); return Create(obj.GetType()); } /// /// Creates a LogHelper to simplify logging. /// /// The target type to log for. /// An instance of this helper public static LogHelper Create(Type type) { return new LogHelper(log4net.LogManager.GetLogger(type)); } /// /// Creates a LogHelper class to simplify logging /// /// The target type to log for. /// An instance of this helper public static LogHelper Create() { return Create(typeof(T)); } /// /// Creates a LogHelper class to simplify logging /// /// The name. /// public static LogHelper Create(string name) { return new LogHelper(log4net.LogManager.GetLogger(name)); } /// /// Holds a reference to the log4net logger /// private readonly ILog logger; public LogHelper(ILog logger) { this.logger = logger; } /// /// Logs fatal messages if ALL, DEBUG, INFO, WARN, ERROR or FATAL is enabled in the config. /// /// The message. /// The exception. public void LogFatal(string message, Exception exception = null) { if (logger != null && logger.IsFatalEnabled) logger.Fatal(message, exception); Add(exception); } /// /// Logs error messages if ALL, DEBUG, INFO, WARN or ERROR level is enabled in the config. /// /// The message. /// The exception. public void LogError(string message, Exception exception = null) { if (logger != null && logger.IsErrorEnabled) logger.Error(message, exception); Add(exception); } /// /// Logs warning messages if ALL, DEBUG, INFO or WARN level is enabled in the config. /// /// The message. /// The exception. public void LogWarning(string message, Exception exception = null) { if (logger != null && logger.IsWarnEnabled) logger.Warn(message, exception); Add(exception); } /// /// Logs info messages if ALL, DEBUG or INFO is enabled in the config. /// /// The message. /// The exception. public void LogInfo(string message, Exception exception = null) { if (logger != null && logger.IsInfoEnabled) logger.Info(message, exception); Add(exception); } /// /// Logs debug messages if ALL or DEBUG is enabled in the config. /// /// The message. /// The exception. public void LogDebug(string message, Exception exception = null) { if (logger != null && logger.IsDebugEnabled) logger.Debug(message, exception); Add(exception); } /// /// Flushes all appenders across the whole logging repository /// /// This call is in most cases not needed public void Flush() { lock(sync) { ILoggerRepository rep = log4net.LogManager.GetRepository(); foreach (IAppender appender in rep.GetAppenders()) { var buffered = appender as BufferingAppenderSkeleton; if (buffered != null) { buffered.Flush(); } } exceptions.Clear(); } } /// /// Clears the internal exception list /// public void Clear() { lock(sync) { exceptions.Clear(); } } /// /// Gets a value indicating whether this instance has logged exceptions. /// /// /// true if this instance has logged exceptions; otherwise, false. /// public bool HasLoggedExceptions { get { return exceptions.Count > 0; } } /// /// Adds the specified exception the internal list. /// /// The exception. private void Add(Exception exception) { if (exception != null) lock (sync) exceptions.Add(exception); } } }