forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogging.log4net.cs
280 lines (236 loc) · 9.57 KB
/
Logging.log4net.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Util;
namespace NHibernate
{
#pragma warning disable 618 // ILoggerFactory is obsolete
/// <summary>
/// Reflection based log4net logger factory.
/// </summary>
public class Log4NetLoggerFactory: ILoggerFactory
#pragma warning restore 618
{
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>();
}
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)
{
return new Log4NetLogger(GetLoggerByNameDelegate(typeof(Log4NetLoggerFactory).Assembly, keyName));
}
[Obsolete("Use this as an INHibernateLoggerFactory instead.")]
public IInternalLogger LoggerFor(System.Type type)
{
return new Log4NetLogger(GetLoggerByTypeDelegate(type));
}
#pragma warning restore 618
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);
return Expression.Lambda<Func<TParameter, object>>(methodCall, resultValue).Compile();
}
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);
return Expression.Lambda<Func<Assembly, string, object>>(methodCall, repositoryAssemblyParam, nameParam).Compile();
}
}
#pragma warning disable 618 // IInternalLogger is obsolete, to be removed in a upcoming major version
/// <summary>
/// Reflection based log4net logger.
/// </summary>
public class Log4NetLogger : IInternalLogger
#pragma warning restore 618
{
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 static readonly Action<object, object> ErrorDelegate;
private static readonly Action<object, object, Exception> ErrorExceptionDelegate;
private static readonly Action<object, string, object[]> ErrorFormatDelegate;
private static readonly Action<object, object> FatalDelegate;
private static readonly Action<object, object, Exception> FatalExceptionDelegate;
private static readonly Action<object, object> DebugDelegate;
private static readonly Action<object, object, Exception> DebugExceptionDelegate;
private static readonly Action<object, string, object[]> DebugFormatDelegate;
private static readonly Action<object, object> InfoDelegate;
private static readonly Action<object, object, Exception> InfoExceptionDelegate;
private static readonly Action<object, string, object[]> InfoFormatDelegate;
private static readonly Action<object, object> WarnDelegate;
private static readonly Action<object, object, Exception> WarnExceptionDelegate;
private static readonly Action<object, string, object[]> WarnFormatDelegate;
private readonly object _logger;
static Log4NetLogger()
{
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>
/// Default constructor.
/// </summary>
/// <param name="logger">The <c>log4net.ILog</c> logger to use for logging.</param>
public Log4NetLogger(object logger)
{
_logger = logger;
}
public bool IsErrorEnabled
{
get { return IsErrorEnabledDelegate(_logger); }
}
public bool IsFatalEnabled
{
get { return IsFatalEnabledDelegate(_logger); }
}
public bool IsDebugEnabled
{
get { return IsDebugEnabledDelegate(_logger); }
}
public bool IsInfoEnabled
{
get { return IsInfoEnabledDelegate(_logger); }
}
public bool IsWarnEnabled
{
get { return IsWarnEnabledDelegate(_logger); }
}
public void Error(object message)
{
if (IsErrorEnabled)
ErrorDelegate(_logger, message);
}
public void Error(object message, Exception exception)
{
if (IsErrorEnabled)
ErrorExceptionDelegate(_logger, message, exception);
}
public void ErrorFormat(string format, params object[] args)
{
if (IsErrorEnabled)
ErrorFormatDelegate(_logger, format, args);
}
public void Fatal(object message)
{
if (IsFatalEnabled)
FatalDelegate(_logger, message);
}
public void Fatal(object message, Exception exception)
{
if (IsFatalEnabled)
FatalExceptionDelegate(_logger, message, exception);
}
public void Debug(object message)
{
if (IsDebugEnabled)
DebugDelegate(_logger, message);
}
public void Debug(object message, Exception exception)
{
if (IsDebugEnabled)
DebugExceptionDelegate(_logger, message, exception);
}
public void DebugFormat(string format, params object[] args)
{
if (IsDebugEnabled)
DebugFormatDelegate(_logger, format, args);
}
public void Info(object message)
{
if (IsInfoEnabled)
InfoDelegate(_logger, message);
}
public void Info(object message, Exception exception)
{
if (IsInfoEnabled)
InfoExceptionDelegate(_logger, message, exception);
}
public void InfoFormat(string format, params object[] args)
{
if (IsInfoEnabled)
InfoFormatDelegate(_logger, format, args);
}
public void Warn(object message)
{
if (IsWarnEnabled)
WarnDelegate(_logger, message);
}
public void Warn(object message, Exception exception)
{
if (IsWarnEnabled)
WarnExceptionDelegate(_logger, message, exception);
}
public void WarnFormat(string format, params object[] args)
{
if (IsWarnEnabled)
WarnFormatDelegate(_logger, format, args);
}
}
}