From: <fab...@us...> - 2010-08-08 23:08:10
|
Revision: 5128 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5128&view=rev Author: fabiomaulo Date: 2010-08-08 23:08:04 +0000 (Sun, 08 Aug 2010) Log Message: ----------- Log4NetLogger without strong-dependency in Log4Net Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Logging.cs Modified: trunk/nhibernate/src/NHibernate/Logging.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Logging.cs 2010-08-08 21:40:41 UTC (rev 5127) +++ trunk/nhibernate/src/NHibernate/Logging.cs 2010-08-08 23:08:04 UTC (rev 5128) @@ -1,4 +1,5 @@ using System; +using System.Linq.Expressions; using log4net; namespace NHibernate @@ -64,130 +65,196 @@ public class Log4NetLogger: ILogger { - private readonly ILog logger; - private readonly Func<ILog, bool> isErrorEnabledDelegate = l => l.IsErrorEnabled; - private readonly Func<ILog, bool> isFatalEnabledDelegate = l => l.IsFatalEnabled; - private readonly Func<ILog, bool> isDebugEnabledDelegate = l => l.IsDebugEnabled; - private readonly Func<ILog, bool> isInfoEnabledDelegate = l => l.IsInfoEnabled; - private readonly Func<ILog, bool> isWarnEnabledDelegate = l => l.IsWarnEnabled; + private static readonly System.Type ILogType = System.Type.GetType("log4net.ILog, log4net"); + private static readonly Func<object, bool> IsErrorEnabledDelegate; + private static readonly Func<object, bool> IsFatalEnabledDelegate; + private static readonly Func<object, bool> IsDebugEnabledDelegate; + private static readonly Func<object, bool> IsInfoEnabledDelegate; + private static readonly Func<object, bool> IsWarnEnabledDelegate; - private readonly Action<ILog, object> errorDelegate= (l,o) => l.Error(o); - private readonly Action<ILog, object,Exception> errorExceptionDelegate=(l,o,e)=> l.Error(o,e); - private readonly Action<ILog, string , object[]> errorFormatDelegate= (l,f,p)=>l.ErrorFormat(f,p); + private static readonly Action<object, object> ErrorDelegate; + private static readonly Action<object, object, Exception> ErrorExceptionDelegate; + private static readonly Action<object, string, object[]> ErrorFormatDelegate; - private readonly Action<ILog, object> fatalDelegate= (l,o) => l.Fatal(o); - private readonly Action<ILog, object,Exception> fatalExceptionDelegate=(l,o,e)=> l.Fatal(o,e); + private static readonly Action<object, object> FatalDelegate; + private static readonly Action<object, object, Exception> FatalExceptionDelegate; - private readonly Action<ILog, object> debugDelegate= (l,o) => l.Debug(o); - private readonly Action<ILog, object, Exception> debugExceptionDelegate = (l, o, e) => l.Debug(o, e); - private readonly Action<ILog, string, object[]> debugFormatDelegate = (l, f, p) => l.DebugFormat(f, p); + private static readonly Action<object, object> DebugDelegate; + private static readonly Action<object, object, Exception> DebugExceptionDelegate; + private static readonly Action<object, string, object[]> DebugFormatDelegate; - private readonly Action<ILog, object> infoDelegate= (l,o) => l.Info(o); - private readonly Action<ILog, object, Exception> infoExceptionDelegate = (l, o, e) => l.Info(o, e); - private readonly Action<ILog, string, object[]> infoFormatDelegate = (l, f, p) => l.InfoFormat(f, p); + private static readonly Action<object, object> InfoDelegate; + private static readonly Action<object, object, Exception> InfoExceptionDelegate; + private static readonly Action<object, string, object[]> InfoFormatDelegate; - private readonly Action<ILog, object> warnDelegate= (l,o) => l.Warn(o); - private readonly Action<ILog, object, Exception> warnExceptionDelegate = (l, o, e) => l.Warn(o, e); - private readonly Action<ILog, string, object[]> warnFormatDelegate = (l, f, p) => l.WarnFormat(f, p); + private static readonly Action<object, object> WarnDelegate; + private static readonly Action<object, object, Exception> WarnExceptionDelegate; + private static readonly Action<object, string, object[]> WarnFormatDelegate; - public Log4NetLogger(ILog logger) + private readonly object logger; + + static Log4NetLogger() { + IsErrorEnabledDelegate = GetPropertyGetter("IsErrorEnabled"); + IsFatalEnabledDelegate = GetPropertyGetter("IsFatalEnabled"); + IsDebugEnabledDelegate = GetPropertyGetter("IsDebugEnabled"); + IsInfoEnabledDelegate = GetPropertyGetter("IsInfoEnabled"); + IsWarnEnabledDelegate = GetPropertyGetter("IsWarnEnabled"); + ErrorDelegate = GetMethodCallForMessage("Error"); + ErrorExceptionDelegate = GetMethodCallForMessageException("Error"); + ErrorFormatDelegate = GetMethodCallForMessageFormat("ErrorFormat"); + + FatalDelegate = GetMethodCallForMessage("Fatal"); + FatalExceptionDelegate = GetMethodCallForMessageException("Fatal"); + + DebugDelegate = GetMethodCallForMessage("Debug"); + DebugExceptionDelegate = GetMethodCallForMessageException("Debug"); + DebugFormatDelegate = GetMethodCallForMessageFormat("DebugFormat"); + + InfoDelegate = GetMethodCallForMessage("Info"); + InfoExceptionDelegate = GetMethodCallForMessageException("Info"); + InfoFormatDelegate = GetMethodCallForMessageFormat("InfoFormat"); + + WarnDelegate = GetMethodCallForMessage("Warn"); + WarnExceptionDelegate = GetMethodCallForMessageException("Warn"); + WarnFormatDelegate = GetMethodCallForMessageFormat("WarnFormat"); + } + + private static Func<object, bool> GetPropertyGetter(string propertyName) + { + ParameterExpression funcParam = Expression.Parameter(typeof(object), "l"); + Expression convertedParam = Expression.Convert(funcParam, ILogType); + Expression property = Expression.Property(convertedParam, propertyName); + return (Func<object, bool>)Expression.Lambda(property, funcParam).Compile(); + } + + private static Action<object, object> GetMethodCallForMessage(string methodName) + { + ParameterExpression loggerParam = Expression.Parameter(typeof(object), "l"); + ParameterExpression messageParam = Expression.Parameter(typeof(object), "o"); + Expression convertedParam = Expression.Convert(loggerParam, ILogType); + MethodCallExpression methodCall = Expression.Call(convertedParam, ILogType.GetMethod(methodName, new[] { typeof(object) }), messageParam); + return (Action<object, object>)Expression.Lambda(methodCall, new[] { loggerParam, messageParam }).Compile(); + } + + private static Action<object, object, Exception> GetMethodCallForMessageException(string methodName) + { + ParameterExpression loggerParam = Expression.Parameter(typeof(object), "l"); + ParameterExpression messageParam = Expression.Parameter(typeof(object), "o"); + ParameterExpression exceptionParam = Expression.Parameter(typeof(Exception), "e"); + Expression convertedParam = Expression.Convert(loggerParam, ILogType); + MethodCallExpression methodCall = Expression.Call(convertedParam, ILogType.GetMethod(methodName, new[] { typeof(object), typeof(Exception) }), messageParam, exceptionParam); + return (Action<object, object, Exception>)Expression.Lambda(methodCall, new[] { loggerParam, messageParam, exceptionParam }).Compile(); + } + + private static Action<object, string, object[]> GetMethodCallForMessageFormat(string methodName) + { + ParameterExpression loggerParam = Expression.Parameter(typeof(object), "l"); + ParameterExpression formatParam = Expression.Parameter(typeof(string), "f"); + ParameterExpression parametersParam = Expression.Parameter(typeof(object[]), "p"); + Expression convertedParam = Expression.Convert(loggerParam, ILogType); + MethodCallExpression methodCall = Expression.Call(convertedParam, ILogType.GetMethod(methodName, new[] { typeof(string), typeof(object[]) }), formatParam, parametersParam); + return (Action<object, string, object[]>)Expression.Lambda(methodCall, new[] { loggerParam, formatParam, parametersParam }).Compile(); + } + + public Log4NetLogger(object logger) + { this.logger = logger; } public bool IsErrorEnabled { - get { return isErrorEnabledDelegate(logger); } + get { return IsErrorEnabledDelegate(logger); } } public bool IsFatalEnabled { - get { return isFatalEnabledDelegate(logger); } + get { return IsFatalEnabledDelegate(logger); } } public bool IsDebugEnabled { - get { return isDebugEnabledDelegate(logger); } + get { return IsDebugEnabledDelegate(logger); } } public bool IsInfoEnabled { - get { return isInfoEnabledDelegate(logger); } + get { return IsInfoEnabledDelegate(logger); } } public bool IsWarnEnabled { - get { return isWarnEnabledDelegate(logger); } + get { return IsWarnEnabledDelegate(logger); } } public void Error(object message) { - errorDelegate(logger, message); + ErrorDelegate(logger, message); } public void Error(object message, Exception exception) { - errorExceptionDelegate(logger,message,exception); + ErrorExceptionDelegate(logger,message,exception); } public void ErrorFormat(string format, params object[] args) { - errorFormatDelegate(logger, format, args); + ErrorFormatDelegate(logger, format, args); } public void Fatal(object message) { - fatalDelegate(logger, message); + FatalDelegate(logger, message); } public void Fatal(object message, Exception exception) { - fatalExceptionDelegate(logger,message,exception); + FatalExceptionDelegate(logger,message,exception); } public void Debug(object message) { - debugDelegate(logger, message); + DebugDelegate(logger, message); } public void Debug(object message, Exception exception) { - debugExceptionDelegate(logger, message, exception); + DebugExceptionDelegate(logger, message, exception); } public void DebugFormat(string format, params object[] args) { - debugFormatDelegate(logger, format, args); + DebugFormatDelegate(logger, format, args); } public void Info(object message) { - infoDelegate(logger, message); + InfoDelegate(logger, message); } public void Info(object message, Exception exception) { - infoExceptionDelegate(logger, message, exception); + InfoExceptionDelegate(logger, message, exception); } public void InfoFormat(string format, params object[] args) { - infoFormatDelegate(logger, format, args); + InfoFormatDelegate(logger, format, args); } public void Warn(object message) { - warnDelegate(logger, message); + WarnDelegate(logger, message); } public void Warn(object message, Exception exception) { - warnExceptionDelegate(logger, message, exception); + WarnExceptionDelegate(logger, message, exception); } public void WarnFormat(string format, params object[] args) { - warnFormatDelegate(logger, format, args); + WarnFormatDelegate(logger, format, args); } } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |