using System;
using System.Linq;
using System.Threading;
using System.Web;
using log4net;
namespace Umbraco.Core.Logging
{
///
/// Used for logging, ILogger should be used instead but this is available for static access to logging
///
///
/// this wraps ILogger
///
public static class LogHelper
{
#region Error
///
/// Adds an error log
///
///
///
///
public static void Error(string message, Exception exception)
{
if (LoggerResolver.HasCurrent == false || LoggerResolver.Current.HasValue == false) return;
LoggerResolver.Current.Logger.Error(message, exception);
}
public static void Error(Type callingType, string message, Exception exception)
{
if (LoggerResolver.HasCurrent == false || LoggerResolver.Current.HasValue == false) return;
LoggerResolver.Current.Logger.Error(callingType, message, exception);
}
#endregion
#region Warn
public static void Warn(Type callingType, string message, params Func[] formatItems)
{
if (LoggerResolver.HasCurrent == false || LoggerResolver.Current.HasValue == false) return;
LoggerResolver.Current.Logger.Warn(callingType, message, formatItems);
}
[Obsolete("Warnings with http trace should not be used. This method will be removed in future versions")]
public static void Warn(Type callingType, string message, bool showHttpTrace, params Func[] formatItems)
{
Mandate.ParameterNotNull(callingType, "callingType");
Mandate.ParameterNotNullOrEmpty(message, "message");
if (showHttpTrace && HttpContext.Current != null)
{
HttpContext.Current.Trace.Warn(callingType.Name, string.Format(message, formatItems.Select(x => x.Invoke()).ToArray()));
}
if (LoggerResolver.HasCurrent == false || LoggerResolver.Current.HasValue == false) return;
LoggerResolver.Current.Logger.Warn(callingType, message, formatItems);
}
public static void WarnWithException(Type callingType, string message, Exception e, params Func[] formatItems)
{
WarnWithException(callingType, message, false, e, formatItems);
}
[Obsolete("Warnings with http trace should not be used. This method will be removed in future versions")]
public static void WarnWithException(Type callingType, string message, bool showHttpTrace, Exception e, params Func[] formatItems)
{
Mandate.ParameterNotNull(e, "e");
Mandate.ParameterNotNull(callingType, "callingType");
Mandate.ParameterNotNullOrEmpty(message, "message");
if (showHttpTrace && HttpContext.Current != null)
{
HttpContext.Current.Trace.Warn(
callingType.Name,
string.Format(message, formatItems.Select(x => x.Invoke()).ToArray()),
e);
}
if (LoggerResolver.HasCurrent == false || LoggerResolver.Current.HasValue == false) return;
LoggerResolver.Current.Logger.WarnWithException(callingType, message, e, formatItems);
}
///
/// Adds a warn log
///
///
///
///
public static void Warn(string message, params Func[] formatItems)
{
Warn(typeof(T), message, formatItems);
}
[Obsolete("Warnings with http trace should not be used. This method will be removed in future versions")]
public static void Warn(string message, bool showHttpTrace, params Func[] formatItems)
{
Warn(typeof(T), message, showHttpTrace, formatItems);
}
public static void WarnWithException(string message, Exception e, params Func[] formatItems)
{
WarnWithException(typeof(T), message, e, formatItems);
}
[Obsolete("Warnings with http trace should not be used. This method will be removed in future versions")]
public static void WarnWithException(string message, bool showHttpTrace, Exception e, params Func[] formatItems)
{
WarnWithException(typeof(T), message, showHttpTrace, e, formatItems);
}
#endregion
#region Info
///
/// Traces a message, only generating the message if tracing is actually enabled. Use this method to avoid calling any long-running methods such as "ToDebugString" if logging is disabled.
///
///
/// The delegate to generate a message.
///
public static void Info(Func generateMessage)
{
Info(typeof(T), generateMessage);
}
///
/// Traces if tracing is enabled.
///
///
///
public static void Info(Type callingType, Func generateMessage)
{
if (LoggerResolver.HasCurrent == false || LoggerResolver.Current.HasValue == false) return;
LoggerResolver.Current.Logger.Info(callingType, generateMessage);
}
///
/// Traces if tracing is enabled.
///
/// The type for the logging namespace.
/// The message format.
/// The format items.
public static void Info(Type type, string generateMessageFormat, params Func[] formatItems)
{
if (LoggerResolver.HasCurrent == false || LoggerResolver.Current.HasValue == false) return;
LoggerResolver.Current.Logger.Info(type, generateMessageFormat, formatItems);
}
///
/// Traces a message, only generating the message if tracing is actually enabled. Use this method to avoid calling any long-running methods such as "ToDebugString" if logging is disabled.
///
///
/// The generate message format.
/// The format items.
///
public static void Info(string generateMessageFormat, params Func[] formatItems)
{
Info(typeof(T), generateMessageFormat, formatItems);
}
#endregion
#region Debug
///
/// Debugs a message, only generating the message if tracing is actually enabled. Use this method to avoid calling any long-running methods such as "ToDebugString" if logging is disabled.
///
///
/// The delegate to generate a message.
///
public static void Debug(Func generateMessage)
{
Debug(typeof(T), generateMessage);
}
///
/// Debugs if tracing is enabled.
///
///
///
public static void Debug(Type callingType, Func generateMessage)
{
if (LoggerResolver.HasCurrent == false || LoggerResolver.Current.HasValue == false) return;
LoggerResolver.Current.Logger.Debug(callingType, generateMessage);
}
///
/// Debugs if tracing is enabled.
///
/// The type for the logging namespace.
/// The message format.
/// The format items.
public static void Debug(Type type, string generateMessageFormat, params Func[] formatItems)
{
if (LoggerResolver.HasCurrent == false || LoggerResolver.Current.HasValue == false) return;
LoggerResolver.Current.Logger.Debug(type, generateMessageFormat, formatItems);
}
///
/// Debugs a message, only generating the message if debug is actually enabled. Use this method to avoid calling any long-running methods such as "ToDebugString" if logging is disabled.
///
///
/// The generate message format.
/// The format items.
///
public static void Debug(string generateMessageFormat, params Func[] formatItems)
{
Debug(typeof(T), generateMessageFormat, formatItems);
}
///
/// Debugs a message and also writes to the TraceContext specified, useful for when you would like the debug
/// output also displayed in the Http trace output.
///
///
///
///
///
[Obsolete("Warnings with http trace should not be used. This method will be removed in future versions")]
public static void Debug(string generateMessageFormat, bool showHttpTrace, params Func[] formatItems)
{
if (showHttpTrace && HttpContext.Current != null)
{
HttpContext.Current.Trace.Write(
typeof(T).Name,
string.Format(generateMessageFormat, formatItems.Select(x => x()).ToArray()));
}
Debug(typeof(T), generateMessageFormat, formatItems);
}
#endregion
}
}