-
Notifications
You must be signed in to change notification settings - Fork 934
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
fredericDelaporte
merged 7 commits into
nhibernate:master
from
fredericDelaporte:log4net
Nov 20, 2018
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
27da04a
Have more robust log4net loading
fredericDelaporte acac0cc
fixup! Have more robust log4net loading
fredericDelaporte 8c5c876
fixup! Have more robust log4net loading
fredericDelaporte 8f4aee8
fixup! Have more robust log4net loading
fredericDelaporte 67bbdd6
Remove exception handling
fredericDelaporte 3fb472e
Merge branch 'master' into log4net
fredericDelaporte bf9c312
Simplify further the change
hazzik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
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() | ||
hazzik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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) | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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; | ||
|
@@ -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> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.