-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLogger.cs
More file actions
357 lines (328 loc) · 12 KB
/
Logger.cs
File metadata and controls
357 lines (328 loc) · 12 KB
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
/* ===============================================
* 功能描述:AspNetCore.Logging.Logger
* 创 建 者:WeiGe
* 创建日期:1/2/2019 9:38:57 PM
* ===============================================*/
using AspNetCore.FileLog;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using LOG = AspNetCore.FileLog;
namespace System
{
/// <summary>
/// Logger
/// </summary>
public partial class Logger : ILogger
{
static readonly Type[] IgnoreTypes = new Type[] {
typeof(Logger),
typeof(Microsoft.Extensions.Logging.LoggerExtensions),
typeof(Microsoft.Extensions.Logging.Logger<>),
typeof( Microsoft.Extensions.Logging.LoggerMessage),
typeof(LogAdapter),
};
private readonly LOG.LoggerFactory _loggerFactory;
private LoggerInformation[] _loggers;
IEnumerable<LogAdapter> _logAdapters;
private int _scopeCount;
IHttpContextAccessor _httpContextAccessor;
internal Logger(LOG.LoggerFactory loggerFactory, IHttpContextAccessor httpContextAccessor, IEnumerable<LogAdapter> logAdapters)
{
_loggerFactory = loggerFactory;
_httpContextAccessor = httpContextAccessor;
_logAdapters = logAdapters;
}
/// <summary>
/// logger save format
/// </summary>
public LogType LogType { get; set; }
internal LoggerInformation[] Loggers
{
get { return _loggers; }
set
{
var scopeSize = 0;
foreach (var loggerInformation in value)
{
if (!loggerInformation.ExternalScope)
{
scopeSize++;
}
}
_scopeCount = scopeSize;
_loggers = value;
}
}
/// <summary>
///
/// </summary>
/// <typeparam name="TState"></typeparam>
/// <param name="logLevel"></param>
/// <param name="eventId"></param>
/// <param name="state"></param>
/// <param name="exception"></param>
/// <param name="formatter"></param>
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state,
Exception exception, Func<TState, Exception, string> formatter)
{
var loggers = Loggers;
if (loggers == null)
{
return;
}
List<Exception> exceptions = null;
bool loged = false;
foreach (var loggerInfo in loggers)
{
if (!loggerInfo.IsEnabled(logLevel))
{
continue;
}
try
{
loggerInfo.Logger.Log(logLevel, eventId, state, exception, formatter);
if (!loged)
{
loged = true;
StackFrame[] stackFrames;
var rule = loggerInfo.Rule;
if (exception == null && rule.LogScope.HasFlag(LogScope.Trace))
{
stackFrames = new StackTrace(false).GetFrames(rule.TraceCount, CanSkip);
}
else
{
stackFrames = Array.Empty<StackFrame>();
}
HttpContext context = null;
if (rule.LogScope.HasFlag(LogScope.HttpContext))
{
context = _httpContextAccessor?.HttpContext;
}
var _format = rule.LogType == LogType.All ? this.LogType : rule.LogType;
foreach (var adapter in _logAdapters)
{
if (adapter.IsEnabled(LogType.Markdown, _format))
{
adapter.Log(rule.CategoryName, eventId.Name,logLevel, rule.LogType, formatter(state, exception), stackFrames, exception, context);
}
if (adapter.IsEnabled(LogType.Database, _format))
{
adapter.Log(rule.CategoryName, eventId.Name, logLevel, rule.LogType, formatter(state, exception), stackFrames, exception, context);
}
if (adapter.IsEnabled(LogType.SystemEvent, _format))
{
adapter.Log(rule.CategoryName, eventId.Name, logLevel, rule.LogType, formatter(state, exception), stackFrames, exception, context);
}
if (adapter.IsEnabled(LogType.Text, _format))
{
adapter.Log(rule.CategoryName, eventId.Name, logLevel, rule.LogType, formatter(state, exception), stackFrames, exception, context);
}
}
}
}
catch (Exception ex)
{
if (exceptions == null)
{
exceptions = new List<Exception>();
}
exceptions.Add(ex);
}
}
if (exceptions != null && exceptions.Count > 0)
{
throw new AggregateException(
message: "An error occurred while writing to logger(s).", innerExceptions: exceptions);
}
}
bool CanSkip(StackFrame frame, MethodBase method)
{
bool canSkip = IgnoreTypes.Contains(method.DeclaringType);
if (!canSkip)
{
var type = method.DeclaringType?.ReflectedType;
if (type != null)
{
canSkip = IgnoreTypes.Contains(type);
}
}
return canSkip;
}
/// <summary>
///
/// </summary>
/// <param name="logLevel"></param>
/// <returns></returns>
public bool IsEnabled(LogLevel logLevel)
{
var loggers = Loggers;
if (loggers == null)
{
return false;
}
List<Exception> exceptions = null;
foreach (var loggerInfo in loggers)
{
if (!loggerInfo.IsEnabled(logLevel))
{
continue;
}
try
{
if (loggerInfo.Logger.IsEnabled(logLevel))
{
return true;
}
}
catch (Exception ex)
{
if (exceptions == null)
{
exceptions = new List<Exception>();
}
exceptions.Add(ex);
}
}
if (exceptions != null && exceptions.Count > 0)
{
throw new AggregateException(
message: "An error occurred while writing to logger(s).",
innerExceptions: exceptions);
}
return false;
}
/// <summary>
///
/// </summary>
/// <typeparam name="TState"></typeparam>
/// <param name="state"></param>
/// <returns></returns>
public IDisposable BeginScope<TState>(TState state)
{
var loggers = Loggers;
if (loggers == null)
{
return NullScope.Instance;
}
var scopeProvider = _loggerFactory.ScopeProvider;
var scopeCount = _scopeCount;
if (scopeProvider != null)
{
// if external scope is used for all providers
// we can return it's IDisposable directly
// without wrapping and saving on allocation
if (scopeCount == 0)
{
return scopeProvider.Push(state);
}
else
{
scopeCount++;
}
}
var scope = new Scope(scopeCount);
List<Exception> exceptions = null;
for (var index = 0; index < loggers.Length; index++)
{
var loggerInformation = loggers[index];
if (loggerInformation.ExternalScope)
{
continue;
}
try
{
scopeCount--;
// _loggers and _scopeCount are not updated atomically
// there might be a situation when count was updated with
// lower value then we have loggers
// This is small race that happens only on configuraiton reload
// and we are protecting from it by checkig that there is enough space
// in Scope
if (scopeCount >= 0)
{
var disposable = loggerInformation.Logger.BeginScope(state);
scope.SetDisposable(scopeCount, disposable);
}
}
catch (Exception ex)
{
if (exceptions == null)
{
exceptions = new List<Exception>();
}
exceptions.Add(ex);
}
}
if (scopeProvider != null)
{
scope.SetDisposable(0, scopeProvider.Push(state));
}
if (exceptions != null && exceptions.Count > 0)
{
throw new AggregateException(
message: "An error occurred while writing to logger(s).", innerExceptions: exceptions);
}
return scope;
}
private class Scope : IDisposable
{
private bool _isDisposed;
private IDisposable _disposable0;
private IDisposable _disposable1;
private readonly IDisposable[] _disposable;
public Scope(int count)
{
if (count > 2)
{
_disposable = new IDisposable[count - 2];
}
}
public void SetDisposable(int index, IDisposable disposable)
{
switch (index)
{
case 0:
_disposable0 = disposable;
break;
case 1:
_disposable1 = disposable;
break;
default:
_disposable[index - 2] = disposable;
break;
}
}
public void Dispose()
{
if (!_isDisposed)
{
_disposable0?.Dispose();
_disposable1?.Dispose();
if (_disposable != null)
{
var count = _disposable.Length;
for (var index = 0; index != count; ++index)
{
if (_disposable[index] != null)
{
_disposable[index].Dispose();
}
}
}
_isDisposed = true;
}
}
}
}
}