// Copyright (C) Stichting Deltares 2023. All rights reserved.
//
// This file is part of the application DAM - Live.
//
// DAM - Live is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
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();
///
/// Holds a reference to the log4net logger
///
private readonly ILog logger;
public LogHelper(ILog logger)
{
this.logger = logger;
}
///
/// 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;
}
}
///
/// 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(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(LogManager.GetLogger(name));
}
///
/// 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 = 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();
}
}
///
/// Adds the specified exception the internal list.
///
/// The exception.
private void Add(Exception exception)
{
if (exception != null)
{
lock (sync)
{
exceptions.Add(exception);
}
}
}
}
}