-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathLogging.cs
433 lines (361 loc) · 12.6 KB
/
Logging.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Util;
namespace NHibernate
{
public interface IInternalLogger
{
bool IsErrorEnabled { get; }
bool IsFatalEnabled { get; }
bool IsDebugEnabled { get; }
bool IsInfoEnabled { get; }
bool IsWarnEnabled { get; }
void Error(object message);
void Error(object message, Exception exception);
void ErrorFormat(string format, params object[] args);
void Fatal(object message);
void Fatal(object message, Exception exception);
void Debug(object message);
void Debug(object message, Exception exception);
void DebugFormat(string format, params object[] args);
void Info(object message);
void Info(object message, Exception exception);
void InfoFormat(string format, params object[] args);
void Warn(object message);
void Warn(object message, Exception exception);
void WarnFormat(string format, params object[] args);
}
public interface ILoggerFactory
{
IInternalLogger LoggerFor(string keyName);
IInternalLogger LoggerFor(System.Type type);
}
public class LoggerProvider
{
private const string NhibernateLoggerConfKey = "nhibernate-logger";
private readonly ILoggerFactory loggerFactory;
private static LoggerProvider instance;
static LoggerProvider()
{
string nhibernateLoggerClass = GetNhibernateLoggerClass();
ILoggerFactory loggerFactory = string.IsNullOrEmpty(nhibernateLoggerClass) ? new NoLoggingLoggerFactory() : GetLoggerFactory(nhibernateLoggerClass);
SetLoggersFactory(loggerFactory);
}
private static ILoggerFactory GetLoggerFactory(string nhibernateLoggerClass)
{
ILoggerFactory loggerFactory;
var loggerFactoryType = System.Type.GetType(nhibernateLoggerClass);
try
{
loggerFactory = (ILoggerFactory) Activator.CreateInstance(loggerFactoryType);
}
catch (MissingMethodException ex)
{
throw new InstantiationException("Public constructor was not found for " + loggerFactoryType, ex, loggerFactoryType);
}
catch (InvalidCastException ex)
{
throw new InstantiationException(loggerFactoryType + "Type does not implement " + typeof (ILoggerFactory), ex, loggerFactoryType);
}
catch (Exception ex)
{
throw new InstantiationException("Unable to instantiate: " + loggerFactoryType, ex, loggerFactoryType);
}
return loggerFactory;
}
private static string GetNhibernateLoggerClass()
{
var nhibernateLogger = ConfigurationManager.AppSettings.Keys.Cast<string>().FirstOrDefault(k => NhibernateLoggerConfKey.Equals(k.ToLowerInvariant()));
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"))
{
nhibernateLoggerClass = typeof (Log4NetLoggerFactory).AssemblyQualifiedName;
}
}
else
{
nhibernateLoggerClass = ConfigurationManager.AppSettings[nhibernateLogger];
}
return nhibernateLoggerClass;
}
public static void SetLoggersFactory(ILoggerFactory loggerFactory)
{
instance = new LoggerProvider(loggerFactory);
}
private LoggerProvider(ILoggerFactory loggerFactory)
{
this.loggerFactory = loggerFactory;
}
public static IInternalLogger LoggerFor(string keyName)
{
return instance.loggerFactory.LoggerFor(keyName);
}
public static IInternalLogger LoggerFor(System.Type type)
{
return instance.loggerFactory.LoggerFor(type);
}
}
public class NoLoggingLoggerFactory: ILoggerFactory
{
private static readonly IInternalLogger Nologging = new NoLoggingInternalLogger();
public IInternalLogger LoggerFor(string keyName)
{
return Nologging;
}
public IInternalLogger LoggerFor(System.Type type)
{
return Nologging;
}
}
public class NoLoggingInternalLogger: IInternalLogger
{
public bool IsErrorEnabled
{
get { return false;}
}
public bool IsFatalEnabled
{
get { return false; }
}
public bool IsDebugEnabled
{
get { return false; }
}
public bool IsInfoEnabled
{
get { return false; }
}
public bool IsWarnEnabled
{
get { return false; }
}
public void Error(object message)
{
}
public void Error(object message, Exception exception)
{
}
public void ErrorFormat(string format, params object[] args)
{
}
public void Fatal(object message)
{
}
public void Fatal(object message, Exception exception)
{
}
public void Debug(object message)
{
}
public void Debug(object message, Exception exception)
{
}
public void DebugFormat(string format, params object[] args)
{
}
public void Info(object message)
{
}
public void Info(object message, Exception exception)
{
}
public void InfoFormat(string format, params object[] args)
{
}
public void Warn(object message)
{
}
public void Warn(object message, Exception exception)
{
}
public void WarnFormat(string format, params object[] args)
{
}
}
public class Log4NetLoggerFactory: ILoggerFactory
{
private static readonly System.Type LogManagerType = System.Type.GetType("log4net.LogManager, log4net");
private static readonly Func<Assembly, string, object> GetLoggerByNameDelegate;
private static readonly Func<System.Type, object> GetLoggerByTypeDelegate;
static Log4NetLoggerFactory()
{
GetLoggerByNameDelegate = GetGetLoggerByNameMethodCall();
GetLoggerByTypeDelegate = GetGetLoggerMethodCall<System.Type>();
}
public IInternalLogger LoggerFor(string keyName)
{
return new Log4NetLogger(GetLoggerByNameDelegate(typeof(Log4NetLoggerFactory).Assembly, keyName));
}
public IInternalLogger LoggerFor(System.Type type)
{
return new Log4NetLogger(GetLoggerByTypeDelegate(type));
}
private static Func<TParameter, object> GetGetLoggerMethodCall<TParameter>()
{
var method = LogManagerType.GetMethod("GetLogger", new[] { 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)});
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();
}
}
public class Log4NetLogger: IInternalLogger
{
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 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()
{
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");
}
public Log4NetLogger(object logger)
{
this.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);
}
}
}