2012-07-28 01:28:39 +06:00
using System ;
using System.Linq ;
using System.Threading ;
2012-08-29 07:47:16 +07:00
using System.Web ;
2012-07-28 01:28:39 +06:00
using log4net ;
2012-07-28 00:13:06 +06:00
2012-07-28 01:28:39 +06:00
namespace Umbraco.Core.Logging
{
2015-01-16 12:07:29 +11:00
///<summary>
2015-01-09 15:27:47 +11:00
/// Used for logging, ILogger should be used instead but this is available for static access to logging
2012-07-28 01:28:39 +06:00
///</summary>
2015-01-07 17:23:24 +11:00
/// <remarks>
/// this wraps ILogger
/// </remarks>
2013-01-03 02:08:29 +03:00
public static class LogHelper
2012-07-28 01:28:39 +06:00
{
2012-07-28 23:40:48 +06:00
#region Error
2012-07-28 01:28:39 +06:00
/// <summary>
/// Adds an error log
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Error < T > ( string message , Exception exception )
{
2015-01-07 17:23:24 +11:00
if ( LoggerResolver . HasCurrent = = false | | LoggerResolver . Current . HasValue = = false ) return ;
LoggerResolver . Current . Logger . Error < T > ( message , exception ) ;
2012-12-10 04:22:29 +05:00
}
public static void Error ( Type callingType , string message , Exception exception )
{
2015-01-07 17:23:24 +11:00
if ( LoggerResolver . HasCurrent = = false | | LoggerResolver . Current . HasValue = = false ) return ;
LoggerResolver . Current . Logger . Error ( callingType , message , exception ) ;
2012-07-28 01:28:39 +06:00
}
2012-12-10 04:22:29 +05:00
2012-07-28 23:40:48 +06:00
#endregion
2012-07-28 00:13:06 +06:00
2012-10-01 23:04:57 +05:00
#region Warn
2012-12-04 11:08:02 +05:00
public static void Warn ( Type callingType , string message , params Func < object > [ ] formatItems )
2012-07-28 01:28:39 +06:00
{
2015-01-07 17:23:24 +11:00
if ( LoggerResolver . HasCurrent = = false | | LoggerResolver . Current . HasValue = = false ) return ;
LoggerResolver . Current . Logger . Warn ( callingType , message , formatItems ) ;
2012-07-28 01:28:39 +06:00
}
2012-07-28 00:13:06 +06:00
2015-01-07 17:23:24 +11:00
[Obsolete("Warnings with http trace should not be used. This method will be removed in future versions")]
2012-12-04 11:08:02 +05:00
public static void Warn ( Type callingType , string message , bool showHttpTrace , params Func < object > [ ] formatItems )
2012-07-28 01:28:39 +06:00
{
2012-12-04 11:08:02 +05:00
Mandate . ParameterNotNull ( callingType , "callingType" ) ;
Mandate . ParameterNotNullOrEmpty ( message , "message" ) ;
if ( showHttpTrace & & HttpContext . Current ! = null )
2012-10-01 23:04:57 +05:00
{
2012-12-04 11:08:02 +05:00
HttpContext . Current . Trace . Warn ( callingType . Name , string . Format ( message , formatItems . Select ( x = > x . Invoke ( ) ) . ToArray ( ) ) ) ;
2015-01-07 17:23:24 +11:00
}
2012-10-01 23:04:57 +05:00
2015-01-07 17:23:24 +11:00
if ( LoggerResolver . HasCurrent = = false | | LoggerResolver . Current . HasValue = = false ) return ;
LoggerResolver . Current . Logger . Warn ( callingType , message , formatItems ) ;
2012-10-01 23:04:57 +05:00
2012-07-28 01:28:39 +06:00
}
2012-07-28 00:13:06 +06:00
2012-12-04 11:08:02 +05:00
public static void WarnWithException ( Type callingType , string message , Exception e , params Func < object > [ ] formatItems )
2012-12-04 05:58:24 +05:00
{
2012-12-04 11:08:02 +05:00
WarnWithException ( callingType , message , false , e , formatItems ) ;
}
2015-01-07 17:23:24 +11:00
[Obsolete("Warnings with http trace should not be used. This method will be removed in future versions")]
2012-12-04 11:08:02 +05:00
public static void WarnWithException ( Type callingType , string message , bool showHttpTrace , Exception e , params Func < object > [ ] formatItems )
{
Mandate . ParameterNotNull ( e , "e" ) ;
Mandate . ParameterNotNull ( callingType , "callingType" ) ;
Mandate . ParameterNotNullOrEmpty ( message , "message" ) ;
if ( showHttpTrace & & HttpContext . Current ! = null )
2012-12-04 05:58:24 +05:00
{
2012-12-04 11:08:02 +05:00
HttpContext . Current . Trace . Warn (
callingType . Name ,
string . Format ( message , formatItems . Select ( x = > x . Invoke ( ) ) . ToArray ( ) ) ,
e ) ;
2012-12-04 05:58:24 +05:00
}
2015-01-07 17:23:24 +11:00
if ( LoggerResolver . HasCurrent = = false | | LoggerResolver . Current . HasValue = = false ) return ;
LoggerResolver . Current . Logger . WarnWithException ( callingType , message , e , formatItems ) ;
2012-12-04 05:58:24 +05:00
}
2012-07-28 01:28:39 +06:00
/// <summary>
/// Adds a warn log
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="message"></param>
2012-12-04 11:08:02 +05:00
/// <param name="formatItems"></param>
public static void Warn < T > ( string message , params Func < object > [ ] formatItems )
2012-07-28 01:28:39 +06:00
{
2012-12-04 11:08:02 +05:00
Warn ( typeof ( T ) , message , formatItems ) ;
2012-07-28 01:28:39 +06:00
}
2012-07-28 00:13:06 +06:00
2015-01-07 17:23:24 +11:00
[Obsolete("Warnings with http trace should not be used. This method will be removed in future versions")]
2012-12-04 11:08:02 +05:00
public static void Warn < T > ( string message , bool showHttpTrace , params Func < object > [ ] formatItems )
2012-07-28 01:28:39 +06:00
{
2012-12-04 11:08:02 +05:00
Warn ( typeof ( T ) , message , showHttpTrace , formatItems ) ;
2012-12-04 05:58:24 +05:00
}
2012-10-01 23:04:57 +05:00
2012-12-04 11:08:02 +05:00
public static void WarnWithException < T > ( string message , Exception e , params Func < object > [ ] formatItems )
{
WarnWithException ( typeof ( T ) , message , e , formatItems ) ;
}
2015-01-07 17:23:24 +11:00
[Obsolete("Warnings with http trace should not be used. This method will be removed in future versions")]
2012-12-04 11:08:02 +05:00
public static void WarnWithException < T > ( string message , bool showHttpTrace , Exception e , params Func < object > [ ] formatItems )
2012-12-04 05:58:24 +05:00
{
2012-12-04 11:08:02 +05:00
WarnWithException ( typeof ( T ) , message , showHttpTrace , e , formatItems ) ;
2012-07-28 23:40:48 +06:00
}
2012-10-01 23:04:57 +05:00
2012-07-28 23:40:48 +06:00
#endregion
2012-07-28 00:13:06 +06:00
2012-07-28 23:40:48 +06:00
#region Info
2012-07-28 01:28:39 +06:00
/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="generateMessage">The delegate to generate a message.</param>
/// <remarks></remarks>
public static void Info < T > ( Func < string > generateMessage )
{
Info ( typeof ( T ) , generateMessage ) ;
}
2012-07-28 00:13:06 +06:00
2012-07-28 23:40:48 +06:00
/// <summary>
/// Traces if tracing is enabled.
/// </summary>
/// <param name="callingType"></param>
/// <param name="generateMessage"></param>
2012-07-28 01:28:39 +06:00
public static void Info ( Type callingType , Func < string > generateMessage )
{
2015-01-07 17:23:24 +11:00
if ( LoggerResolver . HasCurrent = = false | | LoggerResolver . Current . HasValue = = false ) return ;
LoggerResolver . Current . Logger . Info ( callingType , generateMessage ) ;
2012-07-28 01:28:39 +06:00
}
2012-07-28 00:13:06 +06:00
2012-07-28 01:28:39 +06:00
/// <summary>
/// Traces if tracing is enabled.
/// </summary>
/// <param name="type">The type for the logging namespace.</param>
/// <param name="generateMessageFormat">The message format.</param>
/// <param name="formatItems">The format items.</param>
public static void Info ( Type type , string generateMessageFormat , params Func < object > [ ] formatItems )
{
2015-01-07 17:23:24 +11:00
if ( LoggerResolver . HasCurrent = = false | | LoggerResolver . Current . HasValue = = false ) return ;
LoggerResolver . Current . Logger . Info ( type , generateMessageFormat , formatItems ) ;
2012-07-28 01:28:39 +06:00
}
/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="generateMessageFormat">The generate message format.</param>
/// <param name="formatItems">The format items.</param>
/// <remarks></remarks>
public static void Info < T > ( string generateMessageFormat , params Func < object > [ ] formatItems )
{
2012-07-28 23:40:48 +06:00
Info ( typeof ( T ) , generateMessageFormat , formatItems ) ;
}
#endregion
#region Debug
/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="generateMessage">The delegate to generate a message.</param>
/// <remarks></remarks>
public static void Debug < T > ( Func < string > generateMessage )
{
Debug ( typeof ( T ) , generateMessage ) ;
}
/// <summary>
/// Debugs if tracing is enabled.
/// </summary>
/// <param name="callingType"></param>
/// <param name="generateMessage"></param>
public static void Debug ( Type callingType , Func < string > generateMessage )
{
2015-01-07 17:23:24 +11:00
if ( LoggerResolver . HasCurrent = = false | | LoggerResolver . Current . HasValue = = false ) return ;
LoggerResolver . Current . Logger . Debug ( callingType , generateMessage ) ;
2012-07-28 23:40:48 +06:00
}
/// <summary>
/// Debugs if tracing is enabled.
/// </summary>
/// <param name="type">The type for the logging namespace.</param>
/// <param name="generateMessageFormat">The message format.</param>
/// <param name="formatItems">The format items.</param>
public static void Debug ( Type type , string generateMessageFormat , params Func < object > [ ] formatItems )
{
2015-01-07 17:23:24 +11:00
if ( LoggerResolver . HasCurrent = = false | | LoggerResolver . Current . HasValue = = false ) return ;
LoggerResolver . Current . Logger . Debug ( type , generateMessageFormat , formatItems ) ;
2012-07-28 01:28:39 +06:00
}
2012-07-28 23:40:48 +06:00
/// <summary>
2012-08-29 07:47:16 +07:00
/// 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.
2012-07-28 23:40:48 +06:00
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="generateMessageFormat">The generate message format.</param>
/// <param name="formatItems">The format items.</param>
/// <remarks></remarks>
public static void Debug < T > ( string generateMessageFormat , params Func < object > [ ] formatItems )
{
Debug ( typeof ( T ) , generateMessageFormat , formatItems ) ;
}
2012-08-29 07:47:16 +07:00
/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="generateMessageFormat"></param>
2012-12-04 11:08:02 +05:00
/// <param name="showHttpTrace"></param>
2012-08-29 07:47:16 +07:00
/// <param name="formatItems"></param>
2015-01-07 17:23:24 +11:00
[Obsolete("Warnings with http trace should not be used. This method will be removed in future versions")]
2012-12-04 11:08:02 +05:00
public static void Debug < T > ( string generateMessageFormat , bool showHttpTrace , params Func < object > [ ] formatItems )
2012-08-29 07:47:16 +07:00
{
2012-12-04 11:08:02 +05:00
if ( showHttpTrace & & HttpContext . Current ! = null )
2012-09-05 08:01:53 +07:00
{
2012-12-04 11:08:02 +05:00
HttpContext . Current . Trace . Write (
typeof ( T ) . Name ,
string . Format ( generateMessageFormat , formatItems . Select ( x = > x ( ) ) . ToArray ( ) ) ) ;
2012-09-05 08:01:53 +07:00
}
2012-08-29 07:47:16 +07:00
Debug ( typeof ( T ) , generateMessageFormat , formatItems ) ;
}
2012-07-28 23:40:48 +06:00
#endregion
2012-07-28 01:28:39 +06:00
}
}