Skip to content

Have more robust log4net loading #1837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/NHibernate/Logging.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Configuration;
using System.IO;
using System.Linq;

namespace NHibernate
Expand Down Expand Up @@ -117,13 +116,8 @@ private static string GetNhibernateLoggerClass()
string nhibernateLoggerClass = null;
if (string.IsNullOrEmpty(nhibernateLogger))
{
// look for log4net.dll
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string relativeSearchPath = AppDomain.CurrentDomain.RelativeSearchPath;
string binPath = relativeSearchPath == null ? baseDir : Path.Combine(baseDir, relativeSearchPath);
string log4NetDllPath = binPath == null ? "log4net.dll" : Path.Combine(binPath, "log4net.dll");

if (File.Exists(log4NetDllPath) || AppDomain.CurrentDomain.GetAssemblies().Any(a => a.GetName().Name == "log4net"))
// look for log4net
if (Log4NetLoggerFactory.Log4NetAssembly != null)
{
nhibernateLoggerClass = typeof(Log4NetLoggerFactory).AssemblyQualifiedName;
}
Expand Down
92 changes: 67 additions & 25 deletions src/NHibernate/Logging.log4net.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Util;
Expand All @@ -12,16 +13,48 @@ namespace NHibernate
public class Log4NetLoggerFactory: ILoggerFactory
#pragma warning restore 618
{
private static readonly System.Type LogManagerType = System.Type.GetType("log4net.LogManager, log4net");
internal static readonly Assembly Log4NetAssembly;
private static readonly System.Type LogManagerType;
private static readonly Func<Assembly, string, object> GetLoggerByNameDelegate;
private static readonly Func<System.Type, object> GetLoggerByTypeDelegate;

static Log4NetLoggerFactory()
{
LogManagerType = GetLogManagerType();
if (LogManagerType == null)
return;

Log4NetAssembly = LogManagerType.Assembly;
GetLoggerByNameDelegate = GetGetLoggerByNameMethodCall();
GetLoggerByTypeDelegate = GetGetLoggerMethodCall<System.Type>();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method calls following the get type are using the type without checking if it was found, so better not allow GetType to yield null.

}

public Log4NetLoggerFactory()
{
if (LogManagerType == null)
throw new TypeLoadException("Cannot find log4net.LogManager type");
}

// Code adapted from ReflectHelper.TypeFromAssembly, which cannot be called directly due
// to depending on the logger.
private static System.Type GetLogManagerType()
{
var typeName = "log4net.LogManager, log4net";
// Try to get the type from an already loaded assembly
var type = System.Type.GetType(typeName);
if (type != null)
return type;

// Load type from an already loaded assembly, or yield null
return System.Type.GetType(
typeName,
// An alternate could be "a.GetName().Name == an.Name", but GetName() is not lightweight.
// "a.FullName == an.FullName" can never match because an.FullName will lack the version, culture
// and public key token.
an => AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName.StartsWith("log4net,")),
null);
}

#pragma warning disable 618
[Obsolete("Use this as an INHibernateLoggerFactory instead.")]
public IInternalLogger LoggerFor(string keyName)
Expand All @@ -39,6 +72,8 @@ public IInternalLogger LoggerFor(System.Type type)
private static Func<TParameter, object> GetGetLoggerMethodCall<TParameter>()
{
var method = LogManagerType.GetMethod("GetLogger", new[] { typeof(TParameter) });
if (method == null)
throw new InvalidOperationException($"Unable to find LogManager.GetLogger({typeof(TParameter)})");
ParameterExpression resultValue;
ParameterExpression keyParam = Expression.Parameter(typeof(TParameter), "key");
MethodCallExpression methodCall = Expression.Call(null, method, resultValue = keyParam);
Expand All @@ -48,6 +83,8 @@ private static Func<TParameter, object> GetGetLoggerMethodCall<TParameter>()
private static Func<Assembly, string, object> GetGetLoggerByNameMethodCall()
{
var method = LogManagerType.GetMethod("GetLogger", new[] {typeof(Assembly), typeof(string)});
if (method == null)
throw new InvalidOperationException("Unable to find LogManager.GetLogger(Assembly, string)");
ParameterExpression nameParam = Expression.Parameter(typeof(string), "name");
ParameterExpression repositoryAssemblyParam = Expression.Parameter(typeof(Assembly), "repositoryAssembly");
MethodCallExpression methodCall = Expression.Call(null, method, repositoryAssemblyParam, nameParam);
Expand All @@ -62,7 +99,6 @@ private static Func<Assembly, string, object> GetGetLoggerByNameMethodCall()
public class Log4NetLogger : IInternalLogger
#pragma warning restore 618
{
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;
Expand Down Expand Up @@ -92,29 +128,35 @@ public class Log4NetLogger : IInternalLogger

static Log4NetLogger()
{
IsErrorEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(ILogType, "IsErrorEnabled");
IsFatalEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(ILogType, "IsFatalEnabled");
IsDebugEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(ILogType, "IsDebugEnabled");
IsInfoEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(ILogType, "IsInfoEnabled");
IsWarnEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(ILogType, "IsWarnEnabled");
ErrorDelegate = DelegateHelper.BuildAction<object>(ILogType, "Error");
ErrorExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(ILogType, "Error");
ErrorFormatDelegate = DelegateHelper.BuildAction<string, object[]>(ILogType, "ErrorFormat");

FatalDelegate = DelegateHelper.BuildAction<object>(ILogType, "Fatal");
FatalExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(ILogType, "Fatal");

DebugDelegate = DelegateHelper.BuildAction<object>(ILogType, "Debug");
DebugExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(ILogType, "Debug");
DebugFormatDelegate = DelegateHelper.BuildAction<string, object[]>(ILogType, "DebugFormat");

InfoDelegate = DelegateHelper.BuildAction<object>(ILogType, "Info");
InfoExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(ILogType, "Info");
InfoFormatDelegate = DelegateHelper.BuildAction<string, object[]>(ILogType, "InfoFormat");

WarnDelegate = DelegateHelper.BuildAction<object>(ILogType, "Warn");
WarnExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(ILogType, "Warn");
WarnFormatDelegate = DelegateHelper.BuildAction<string, object[]>(ILogType, "WarnFormat");
if (Log4NetLoggerFactory.Log4NetAssembly == null)
throw new TypeLoadException(
"Cannot load ILog type, log4net is not found");

var iLogType = Log4NetLoggerFactory.Log4NetAssembly.GetType("log4net.ILog", true);

IsErrorEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(iLogType, "IsErrorEnabled");
IsFatalEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(iLogType, "IsFatalEnabled");
IsDebugEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(iLogType, "IsDebugEnabled");
IsInfoEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(iLogType, "IsInfoEnabled");
IsWarnEnabledDelegate = DelegateHelper.BuildPropertyGetter<bool>(iLogType, "IsWarnEnabled");
ErrorDelegate = DelegateHelper.BuildAction<object>(iLogType, "Error");
ErrorExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(iLogType, "Error");
ErrorFormatDelegate = DelegateHelper.BuildAction<string, object[]>(iLogType, "ErrorFormat");

FatalDelegate = DelegateHelper.BuildAction<object>(iLogType, "Fatal");
FatalExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(iLogType, "Fatal");

DebugDelegate = DelegateHelper.BuildAction<object>(iLogType, "Debug");
DebugExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(iLogType, "Debug");
DebugFormatDelegate = DelegateHelper.BuildAction<string, object[]>(iLogType, "DebugFormat");

InfoDelegate = DelegateHelper.BuildAction<object>(iLogType, "Info");
InfoExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(iLogType, "Info");
InfoFormatDelegate = DelegateHelper.BuildAction<string, object[]>(iLogType, "InfoFormat");

WarnDelegate = DelegateHelper.BuildAction<object>(iLogType, "Warn");
WarnExceptionDelegate = DelegateHelper.BuildAction<object, Exception>(iLogType, "Warn");
WarnFormatDelegate = DelegateHelper.BuildAction<string, object[]>(iLogType, "WarnFormat");
}

/// <summary>
Expand Down