forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogging.cs
265 lines (240 loc) · 7.94 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
using System;
using NHibernate.Cfg;
using System.Runtime.CompilerServices;
namespace NHibernate
{
public interface INHibernateLogger
{
/// <summary>Writes a log entry.</summary>
/// <param name="logLevel">Entry will be written on this level.</param>
/// <param name="state">The entry to be written.</param>
/// <param name="exception">The exception related to this entry.</param>
void Log(NHibernateLogLevel logLevel, NHibernateLogValues state, Exception exception);
/// <summary>
/// Checks if the given <paramref name="logLevel" /> is enabled.
/// </summary>
/// <param name="logLevel">level to be checked.</param>
/// <returns><c>true</c> if enabled.</returns>
bool IsEnabled(NHibernateLogLevel logLevel);
}
/// <summary>
/// Factory interface for providing a <see cref="INHibernateLogger"/>.
/// </summary>
public interface INHibernateLoggerFactory
{
/// <summary>
/// Get a logger for the given log key.
/// </summary>
/// <param name="keyName">The log key.</param>
/// <returns>A NHibernate logger.</returns>
INHibernateLogger LoggerFor(string keyName);
/// <summary>
/// Get a logger using the given type as log key.
/// </summary>
/// <param name="type">The type to use as log key.</param>
/// <returns>A NHibernate logger.</returns>
INHibernateLogger LoggerFor(System.Type type);
}
/// <summary>
/// Provide methods for getting NHibernate loggers according to supplied <see cref="INHibernateLoggerFactory"/>.
/// </summary>
/// <remarks>
/// By default, it will use a <see cref="Log4NetLoggerFactory"/> if log4net is available, otherwise it will
/// use a <see cref="NoLoggingNHibernateLoggerFactory"/>.
/// </remarks>
public static class NHibernateLogger
{
private static INHibernateLoggerFactory _loggerFactory;
#pragma warning disable 618
private static ILoggerFactory _legacyLoggerFactory;
internal static ILoggerFactory LegacyLoggerFactory => LogWrapper.LegacyLoggerFactory;
#pragma warning restore 618
private static class LogWrapper
{
static LogWrapper()
{
var userLoggerFactory = _loggerFactory;
if (userLoggerFactory == null)
{
var nhibernateLoggerClass = GetNhibernateLoggerClass();
var loggerFactory = string.IsNullOrEmpty(nhibernateLoggerClass) ? null : GetLoggerFactory(nhibernateLoggerClass);
SetLoggersFactory(loggerFactory);
}
}
public static INHibernateLoggerFactory LoggerFactory
{
[MethodImpl(MethodImplOptions.NoInlining)]
get => _loggerFactory;
}
#pragma warning disable 618
internal static ILoggerFactory LegacyLoggerFactory
{
[MethodImpl(MethodImplOptions.NoInlining)]
get => _legacyLoggerFactory;
}
#pragma warning restore 618
}
/// <summary>
/// Specify the logger factory to use for building loggers.
/// </summary>
/// <param name="loggerFactory">A logger factory.</param>
public static void SetLoggersFactory(INHibernateLoggerFactory loggerFactory)
{
_loggerFactory = loggerFactory ?? new NoLoggingNHibernateLoggerFactory();
#pragma warning disable 618
// Also keep global state for obsolete logger
if (loggerFactory == null)
{
_legacyLoggerFactory = new NoLoggingLoggerFactory();
}
else
{
if (loggerFactory is LoggerProvider.LegacyLoggerFactoryAdaptor legacyAdaptor)
{
_legacyLoggerFactory = legacyAdaptor.Factory;
}
else
{
_legacyLoggerFactory = new LoggerProvider.ReverseLegacyLoggerFactoryAdaptor(loggerFactory);
}
}
#pragma warning restore 618
}
/// <summary>
/// Get a logger for the given log key.
/// </summary>
/// <param name="keyName">The log key.</param>
/// <returns>A NHibernate logger.</returns>
public static INHibernateLogger For(string keyName)
{
return LogWrapper.LoggerFactory.LoggerFor(keyName);
}
/// <summary>
/// Get a logger using the given type as log key.
/// </summary>
/// <param name="type">The type to use as log key.</param>
/// <returns>A NHibernate logger.</returns>
public static INHibernateLogger For(System.Type type)
{
return LogWrapper.LoggerFactory.LoggerFor(type);
}
private static string GetNhibernateLoggerClass()
{
var nhibernateLoggerClass = ConfigurationProvider.Current.GetLoggerFactoryClassName();
if (nhibernateLoggerClass == null)
{
// look for log4net
if (Log4NetLoggerFactory.Log4NetAssembly != null)
{
nhibernateLoggerClass = typeof(Log4NetLoggerFactory).AssemblyQualifiedName;
}
}
return nhibernateLoggerClass;
}
private static INHibernateLoggerFactory GetLoggerFactory(string nhibernateLoggerClass)
{
INHibernateLoggerFactory loggerFactory;
var loggerFactoryType = System.Type.GetType(nhibernateLoggerClass);
try
{
var loadedLoggerFactory = Activator.CreateInstance(loggerFactoryType);
#pragma warning disable 618
if (loadedLoggerFactory is ILoggerFactory oldStyleFactory)
{
loggerFactory = new LoggerProvider.LegacyLoggerFactoryAdaptor(oldStyleFactory);
}
#pragma warning restore 618
else
{
loggerFactory = (INHibernateLoggerFactory) loadedLoggerFactory;
}
}
catch (MissingMethodException ex)
{
throw new InstantiationException("Public constructor was not found for " + loggerFactoryType, ex, loggerFactoryType);
}
catch (InvalidCastException ex)
{
#pragma warning disable 618
throw new InstantiationException(loggerFactoryType + "Type does not implement " + typeof(INHibernateLoggerFactory) + " or " + typeof(ILoggerFactory), ex, loggerFactoryType);
#pragma warning restore 618
}
catch (Exception ex)
{
throw new InstantiationException("Unable to instantiate: " + loggerFactoryType, ex, loggerFactoryType);
}
return loggerFactory;
}
}
internal class NoLoggingNHibernateLoggerFactory: INHibernateLoggerFactory
{
private static readonly INHibernateLogger Nologging = new NoLoggingNHibernateLogger();
public INHibernateLogger LoggerFor(string keyName)
{
return Nologging;
}
public INHibernateLogger LoggerFor(System.Type type)
{
return Nologging;
}
}
internal class NoLoggingNHibernateLogger: INHibernateLogger
{
public void Log(NHibernateLogLevel logLevel, NHibernateLogValues state, Exception exception)
{
}
public bool IsEnabled(NHibernateLogLevel logLevel)
{
if (logLevel == NHibernateLogLevel.None) return true;
return false;
}
}
public struct NHibernateLogValues
{
private readonly string _format;
private readonly object[] _args;
/// <summary>
/// Instantiates a new instance of the <see cref="NHibernateLogValues"/> structure.
/// </summary>
/// <param name="format">A composite format string</param>
/// <param name="args">An object array that contains zero or more objects to format. Can be <c>null</c> if there are no values to format.</param>
public NHibernateLogValues(string format, object[] args)
{
_format = format ?? "[Null]";
_args = args ?? Array.Empty<object>();
}
/// <summary>
/// Returns the composite format string.
/// </summary>
/// <remarks>
/// A composite format string consists of zero or more runs of fixed text intermixed with
/// one or more format items, which are indicated by an index number delimited with brackets
/// (for example, {0}). The index of each format item corresponds to an argument in an object
/// list that follows the composite format string.
/// </remarks>
public string Format => _format;
/// <summary>
/// An object array that contains zero or more objects to format. Can be <c>null</c> if there are no values to format.
/// </summary>
public object[] Args => _args;
/// <summary>
/// Returns the string that results from formatting the composite format string along with
/// its arguments by using the formatting conventions of the current culture.
/// </summary>
public override string ToString()
{
return _args?.Length > 0 ? string.Format(_format, _args) : Format;
}
}
/// <summary>Defines logging severity levels.</summary>
public enum NHibernateLogLevel
{
Trace,
Debug,
Info,
Warn,
Error,
Fatal,
None,
}
}