/
Logger.cs
722 lines (663 loc) · 32.7 KB
/
Logger.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
//
// Copyright (c) 2004-2021 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
namespace NLog
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
#if NET4_5
using System.Threading.Tasks;
#endif
using JetBrains.Annotations;
using NLog.Internal;
/// <summary>
/// Provides logging interface and utility functions.
/// </summary>
[CLSCompliant(true)]
public partial class Logger : ILogger
{
internal static readonly Type DefaultLoggerType = typeof(Logger);
private Logger _contextLogger;
private ThreadSafeDictionary<string, object> _contextProperties;
private LoggerConfiguration _configuration;
private volatile bool _isTraceEnabled;
private volatile bool _isDebugEnabled;
private volatile bool _isInfoEnabled;
private volatile bool _isWarnEnabled;
private volatile bool _isErrorEnabled;
private volatile bool _isFatalEnabled;
/// <summary>
/// Initializes a new instance of the <see cref="Logger"/> class.
/// </summary>
protected internal Logger()
{
_contextLogger = this;
}
/// <summary>
/// Occurs when logger configuration changes.
/// </summary>
public event EventHandler<EventArgs> LoggerReconfigured;
/// <summary>
/// Gets the name of the logger.
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Gets the factory that created this logger.
/// </summary>
public LogFactory Factory { get; private set; }
/// <summary>
/// Collection of context properties for the Logger. The logger will append it for all log events
/// </summary>
/// <remarks>
/// It is recommended to use <see cref="WithProperty(string, object)"/> for modifying context properties
/// when same named logger is used at multiple locations or shared by different thread contexts.
/// </remarks>
public IDictionary<string, object> Properties => _contextProperties ?? System.Threading.Interlocked.CompareExchange(ref _contextProperties, CreateContextPropertiesDictionary(null), null) ?? _contextProperties;
/// <summary>
/// Gets a value indicating whether logging is enabled for the specified level.
/// </summary>
/// <param name="level">Log level to be checked.</param>
/// <returns>A value of <see langword="true" /> if logging is enabled for the specified level, otherwise it returns <see langword="false" />.</returns>
public bool IsEnabled(LogLevel level)
{
if (level == null)
{
throw new InvalidOperationException("Log level must be defined");
}
return GetTargetsForLevel(level) != null;
}
/// <summary>
/// Creates new logger that automatically appends the specified property to all log events (without changing current logger)
/// </summary>
/// <param name="propertyKey">Property Name</param>
/// <param name="propertyValue">Property Value</param>
/// <returns>New Logger object that automatically appends specified property</returns>
public Logger WithProperty(string propertyKey, object propertyValue)
{
if (string.IsNullOrEmpty(propertyKey))
throw new ArgumentException(nameof(propertyKey));
Logger newLogger = Factory.CreateNewLogger(GetType()) ?? new Logger();
newLogger.Initialize(Name, _configuration, Factory);
newLogger._contextProperties = CreateContextPropertiesDictionary(_contextProperties);
newLogger._contextProperties[propertyKey] = propertyValue;
newLogger._contextLogger = _contextLogger; // Use the LoggerConfiguration of the parent Logger
return newLogger;
}
/// <summary>
/// Updates the specified context property for the current logger. The logger will append it for all log events.
///
/// It could be rendered with ${event-properties:YOURNAME}
///
/// With <see cref="Properties"/> property, all properties could be changed.
/// </summary>
/// <remarks>
/// Will affect all locations/contexts that makes use of the same named logger object.
/// </remarks>
/// <param name="propertyKey">Property Name</param>
/// <param name="propertyValue">Property Value</param>
/// <remarks>
/// It is recommended to use <see cref="WithProperty(string, object)"/> for modifying context properties
/// when same named logger is used at multiple locations or shared by different thread contexts.
/// </remarks>
public void SetProperty(string propertyKey, object propertyValue)
{
if (string.IsNullOrEmpty(propertyKey))
throw new ArgumentException(nameof(propertyKey));
Properties[propertyKey] = propertyValue;
}
private static ThreadSafeDictionary<string, object> CreateContextPropertiesDictionary(ThreadSafeDictionary<string, object> contextProperties)
{
contextProperties = contextProperties != null
? new ThreadSafeDictionary<string, object>(contextProperties)
: new ThreadSafeDictionary<string, object>();
return contextProperties;
}
/// <summary>
/// Writes the specified diagnostic message.
/// </summary>
/// <param name="logEvent">Log event.</param>
public void Log(LogEventInfo logEvent)
{
var targetsForLevel = IsEnabled(logEvent.Level) ? GetTargetsForLevel(logEvent.Level) : null;
if (targetsForLevel != null)
{
if (logEvent.LoggerName == null)
logEvent.LoggerName = Name;
WriteToTargets(logEvent, targetsForLevel);
}
}
/// <summary>
/// Writes the specified diagnostic message.
/// </summary>
/// <param name="wrapperType">The name of the type that wraps Logger.</param>
/// <param name="logEvent">Log event.</param>
public void Log(Type wrapperType, LogEventInfo logEvent)
{
var targetsForLevel = IsEnabled(logEvent.Level) ? GetTargetsForLevel(logEvent.Level) : null;
if (targetsForLevel != null)
{
if (logEvent.LoggerName == null)
logEvent.LoggerName = Name;
WriteToTargets(wrapperType, logEvent, targetsForLevel);
}
}
#region Log() overloads
/// <overloads>
/// Writes the diagnostic message at the specified level using the specified format provider and format parameters.
/// </overloads>
/// <summary>
/// Writes the diagnostic message at the specified level.
/// </summary>
/// <typeparam name="T">Type of the value.</typeparam>
/// <param name="level">The log level.</param>
/// <param name="value">The value to be written.</param>
public void Log<T>(LogLevel level, T value)
{
if (IsEnabled(level))
{
WriteToTargets(level, null, value);
}
}
/// <summary>
/// Writes the diagnostic message at the specified level.
/// </summary>
/// <typeparam name="T">Type of the value.</typeparam>
/// <param name="level">The log level.</param>
/// <param name="formatProvider">An IFormatProvider that supplies culture-specific formatting information.</param>
/// <param name="value">The value to be written.</param>
public void Log<T>(LogLevel level, IFormatProvider formatProvider, T value)
{
if (IsEnabled(level))
{
WriteToTargets(level, formatProvider, value);
}
}
/// <summary>
/// Writes the diagnostic message at the specified level.
/// </summary>
/// <param name="level">The log level.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
public void Log(LogLevel level, LogMessageGenerator messageFunc)
{
if (IsEnabled(level))
{
if (messageFunc == null)
{
throw new ArgumentNullException(nameof(messageFunc));
}
WriteToTargets(level, null, messageFunc());
}
}
/// <summary>
/// Writes the diagnostic message and exception at the specified level.
/// </summary>
/// <param name="level">The log level.</param>
/// <param name="message">A <see langword="string" /> to be written.</param>
/// <param name="exception">An exception to be logged.</param>
/// <remarks>This method was marked as obsolete before NLog 4.3.11 and it may be removed in a future release.</remarks>
[Obsolete("Use Log(LogLevel, String, Exception) method instead. Marked obsolete before v4.3.11")]
public void LogException(LogLevel level, [Localizable(false)] string message, Exception exception)
{
Log(level, message, exception);
}
/// <summary>
/// Writes the diagnostic message at the specified level using the specified parameters and formatting them with the supplied format provider.
/// </summary>
/// <param name="level">The log level.</param>
/// <param name="formatProvider">An IFormatProvider that supplies culture-specific formatting information.</param>
/// <param name="message">A <see langword="string" /> containing format items.</param>
/// <param name="args">Arguments to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log(LogLevel level, IFormatProvider formatProvider, [Localizable(false)] string message, params object[] args)
{
if (IsEnabled(level))
{
WriteToTargets(level, formatProvider, message, args);
}
}
/// <summary>
/// Writes the diagnostic message at the specified level.
/// </summary>
/// <param name="level">The log level.</param>
/// <param name="message">Log message.</param>
public void Log(LogLevel level, [Localizable(false)] string message)
{
if (IsEnabled(level))
{
WriteToTargets(level, null, message);
}
}
/// <summary>
/// Writes the diagnostic message at the specified level using the specified parameters.
/// </summary>
/// <param name="level">The log level.</param>
/// <param name="message">A <see langword="string" /> containing format items.</param>
/// <param name="args">Arguments to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log(LogLevel level, [Localizable(false)] string message, params object[] args)
{
if (IsEnabled(level))
{
WriteToTargets(level, message, args);
}
}
/// <summary>
/// Writes the diagnostic message and exception at the specified level.
/// </summary>
/// <param name="level">The log level.</param>
/// <param name="message">A <see langword="string" /> to be written.</param>
/// <param name="exception">An exception to be logged.</param>
/// <remarks>This method was marked as obsolete before NLog 4.3.11 and it may be removed in a future release.</remarks>
[Obsolete("Use Log(LogLevel level, Exception exception, [Localizable(false)] string message, params object[] args) instead. Marked obsolete before v4.3.11")]
public void Log(LogLevel level, [Localizable(false)] string message, Exception exception)
{
if (IsEnabled(level))
{
WriteToTargets(level, exception, message, null);
}
}
/// <summary>
/// Writes the diagnostic message and exception at the specified level.
/// </summary>
/// <param name="level">The log level.</param>
/// <param name="message">A <see langword="string" /> to be written.</param>
/// <param name="args">Arguments to format.</param>
/// <param name="exception">An exception to be logged.</param>
[MessageTemplateFormatMethod("message")]
public void Log(LogLevel level, Exception exception, [Localizable(false)] string message, params object[] args)
{
if (IsEnabled(level))
{
WriteToTargets(level, exception, message, args);
}
}
/// <summary>
/// Writes the diagnostic message and exception at the specified level.
/// </summary>
/// <param name="level">The log level.</param>
/// <param name="formatProvider">An IFormatProvider that supplies culture-specific formatting information.</param>
/// <param name="message">A <see langword="string" /> to be written.</param>
/// <param name="args">Arguments to format.</param>
/// <param name="exception">An exception to be logged.</param>
[MessageTemplateFormatMethod("message")]
public void Log(LogLevel level, Exception exception, IFormatProvider formatProvider, [Localizable(false)] string message, params object[] args)
{
if (IsEnabled(level))
{
WriteToTargets(level, exception, formatProvider, message, args);
}
}
/// <summary>
/// Writes the diagnostic message at the specified level using the specified parameter and formatting it with the supplied format provider.
/// </summary>
/// <typeparam name="TArgument">The type of the argument.</typeparam>
/// <param name="level">The log level.</param>
/// <param name="formatProvider">An IFormatProvider that supplies culture-specific formatting information.</param>
/// <param name="message">A <see langword="string" /> containing one format item.</param>
/// <param name="argument">The argument to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log<TArgument>(LogLevel level, IFormatProvider formatProvider, [Localizable(false)] string message, TArgument argument)
{
if (IsEnabled(level))
{
WriteToTargets(level, formatProvider, message, new object[] { argument });
}
}
/// <summary>
/// Writes the diagnostic message at the specified level using the specified parameter.
/// </summary>
/// <typeparam name="TArgument">The type of the argument.</typeparam>
/// <param name="level">The log level.</param>
/// <param name="message">A <see langword="string" /> containing one format item.</param>
/// <param name="argument">The argument to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log<TArgument>(LogLevel level, [Localizable(false)] string message, TArgument argument)
{
if (IsEnabled(level))
{
WriteToTargets(level, message, new object[] { argument });
}
}
/// <summary>
/// Writes the diagnostic message at the specified level using the specified arguments formatting it with the supplied format provider.
/// </summary>
/// <typeparam name="TArgument1">The type of the first argument.</typeparam>
/// <typeparam name="TArgument2">The type of the second argument.</typeparam>
/// <param name="level">The log level.</param>
/// <param name="formatProvider">An IFormatProvider that supplies culture-specific formatting information.</param>
/// <param name="message">A <see langword="string" /> containing one format item.</param>
/// <param name="argument1">The first argument to format.</param>
/// <param name="argument2">The second argument to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log<TArgument1, TArgument2>(LogLevel level, IFormatProvider formatProvider, [Localizable(false)] string message, TArgument1 argument1, TArgument2 argument2)
{
if (IsEnabled(level))
{
WriteToTargets(level, formatProvider, message, new object[] { argument1, argument2 });
}
}
/// <summary>
/// Writes the diagnostic message at the specified level using the specified parameters.
/// </summary>
/// <typeparam name="TArgument1">The type of the first argument.</typeparam>
/// <typeparam name="TArgument2">The type of the second argument.</typeparam>
/// <param name="level">The log level.</param>
/// <param name="message">A <see langword="string" /> containing one format item.</param>
/// <param name="argument1">The first argument to format.</param>
/// <param name="argument2">The second argument to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log<TArgument1, TArgument2>(LogLevel level, [Localizable(false)] string message, TArgument1 argument1, TArgument2 argument2)
{
if (IsEnabled(level))
{
WriteToTargets(level, message, new object[] { argument1, argument2 });
}
}
/// <summary>
/// Writes the diagnostic message at the specified level using the specified arguments formatting it with the supplied format provider.
/// </summary>
/// <typeparam name="TArgument1">The type of the first argument.</typeparam>
/// <typeparam name="TArgument2">The type of the second argument.</typeparam>
/// <typeparam name="TArgument3">The type of the third argument.</typeparam>
/// <param name="level">The log level.</param>
/// <param name="formatProvider">An IFormatProvider that supplies culture-specific formatting information.</param>
/// <param name="message">A <see langword="string" /> containing one format item.</param>
/// <param name="argument1">The first argument to format.</param>
/// <param name="argument2">The second argument to format.</param>
/// <param name="argument3">The third argument to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log<TArgument1, TArgument2, TArgument3>(LogLevel level, IFormatProvider formatProvider, [Localizable(false)] string message, TArgument1 argument1, TArgument2 argument2, TArgument3 argument3)
{
if (IsEnabled(level))
{
WriteToTargets(level, formatProvider, message, new object[] { argument1, argument2, argument3 });
}
}
/// <summary>
/// Writes the diagnostic message at the specified level using the specified parameters.
/// </summary>
/// <typeparam name="TArgument1">The type of the first argument.</typeparam>
/// <typeparam name="TArgument2">The type of the second argument.</typeparam>
/// <typeparam name="TArgument3">The type of the third argument.</typeparam>
/// <param name="level">The log level.</param>
/// <param name="message">A <see langword="string" /> containing one format item.</param>
/// <param name="argument1">The first argument to format.</param>
/// <param name="argument2">The second argument to format.</param>
/// <param name="argument3">The third argument to format.</param>
[MessageTemplateFormatMethod("message")]
public void Log<TArgument1, TArgument2, TArgument3>(LogLevel level, [Localizable(false)] string message, TArgument1 argument1, TArgument2 argument2, TArgument3 argument3)
{
if (IsEnabled(level))
{
WriteToTargets(level, message, new object[] { argument1, argument2, argument3 });
}
}
private LogEventInfo PrepareLogEventInfo(LogEventInfo logEvent)
{
if (logEvent.FormatProvider == null)
{
logEvent.FormatProvider = Factory.DefaultCultureInfo;
}
if (_contextProperties != null)
{
foreach (var property in _contextProperties)
{
if (!logEvent.Properties.ContainsKey(property.Key))
{
logEvent.Properties[property.Key] = property.Value;
}
}
}
return logEvent;
}
#endregion
/// <summary>
/// Runs the provided action. If the action throws, the exception is logged at <c>Error</c> level. The exception is not propagated outside of this method.
/// </summary>
/// <param name="action">Action to execute.</param>
public void Swallow(Action action)
{
try
{
action();
}
catch (Exception e)
{
Error(e);
}
}
/// <summary>
/// Runs the provided function and returns its result. If an exception is thrown, it is logged at <c>Error</c> level.
/// The exception is not propagated outside of this method; a default value is returned instead.
/// </summary>
/// <typeparam name="T">Return type of the provided function.</typeparam>
/// <param name="func">Function to run.</param>
/// <returns>Result returned by the provided function or the default value of type <typeparamref name="T"/> in case of exception.</returns>
public T Swallow<T>(Func<T> func)
{
return Swallow(func, default(T));
}
/// <summary>
/// Runs the provided function and returns its result. If an exception is thrown, it is logged at <c>Error</c> level.
/// The exception is not propagated outside of this method; a fallback value is returned instead.
/// </summary>
/// <typeparam name="T">Return type of the provided function.</typeparam>
/// <param name="func">Function to run.</param>
/// <param name="fallback">Fallback value to return in case of exception.</param>
/// <returns>Result returned by the provided function or fallback value in case of exception.</returns>
public T Swallow<T>(Func<T> func, T fallback)
{
try
{
return func();
}
catch (Exception e)
{
Error(e);
return fallback;
}
}
#if NET4_5
/// <summary>
/// Logs an exception is logged at <c>Error</c> level if the provided task does not run to completion.
/// </summary>
/// <param name="task">The task for which to log an error if it does not run to completion.</param>
/// <remarks>This method is useful in fire-and-forget situations, where application logic does not depend on completion of task. This method is avoids C# warning CS4014 in such situations.</remarks>
public async void Swallow(Task task)
{
try
{
await task;
}
catch (Exception e)
{
Error(e);
}
}
/// <summary>
/// Returns a task that completes when a specified task to completes. If the task does not run to completion, an exception is logged at <c>Error</c> level. The returned task always runs to completion.
/// </summary>
/// <param name="task">The task for which to log an error if it does not run to completion.</param>
/// <returns>A task that completes in the <see cref="TaskStatus.RanToCompletion"/> state when <paramref name="task"/> completes.</returns>
public async Task SwallowAsync(Task task)
{
try
{
await task;
}
catch (Exception e)
{
Error(e);
}
}
/// <summary>
/// Runs async action. If the action throws, the exception is logged at <c>Error</c> level. The exception is not propagated outside of this method.
/// </summary>
/// <param name="asyncAction">Async action to execute.</param>
public async Task SwallowAsync(Func<Task> asyncAction)
{
try
{
await asyncAction();
}
catch (Exception e)
{
Error(e);
}
}
/// <summary>
/// Runs the provided async function and returns its result. If the task does not run to completion, an exception is logged at <c>Error</c> level.
/// The exception is not propagated outside of this method; a default value is returned instead.
/// </summary>
/// <typeparam name="TResult">Return type of the provided function.</typeparam>
/// <param name="asyncFunc">Async function to run.</param>
/// <returns>A task that represents the completion of the supplied task. If the supplied task ends in the <see cref="TaskStatus.RanToCompletion"/> state, the result of the new task will be the result of the supplied task; otherwise, the result of the new task will be the default value of type <typeparamref name="TResult"/>.</returns>
public async Task<TResult> SwallowAsync<TResult>(Func<Task<TResult>> asyncFunc)
{
return await SwallowAsync(asyncFunc, default(TResult));
}
/// <summary>
/// Runs the provided async function and returns its result. If the task does not run to completion, an exception is logged at <c>Error</c> level.
/// The exception is not propagated outside of this method; a fallback value is returned instead.
/// </summary>
/// <typeparam name="TResult">Return type of the provided function.</typeparam>
/// <param name="asyncFunc">Async function to run.</param>
/// <param name="fallback">Fallback value to return if the task does not end in the <see cref="TaskStatus.RanToCompletion"/> state.</param>
/// <returns>A task that represents the completion of the supplied task. If the supplied task ends in the <see cref="TaskStatus.RanToCompletion"/> state, the result of the new task will be the result of the supplied task; otherwise, the result of the new task will be the fallback value.</returns>
public async Task<TResult> SwallowAsync<TResult>(Func<Task<TResult>> asyncFunc, TResult fallback)
{
try
{
return await asyncFunc();
}
catch (Exception e)
{
Error(e);
return fallback;
}
}
#endif
internal void Initialize(string name, LoggerConfiguration loggerConfiguration, LogFactory factory)
{
Name = name;
Factory = factory;
SetConfiguration(loggerConfiguration);
}
private void WriteToTargets(LogLevel level, [Localizable(false)] string message, object[] args)
{
WriteToTargets(level, Factory.DefaultCultureInfo, message, args);
}
private void WriteToTargets(LogLevel level, IFormatProvider formatProvider, [Localizable(false)] string message, object[] args)
{
var targetsForLevel = GetTargetsForLevel(level);
if (targetsForLevel != null)
{
var logEvent = LogEventInfo.Create(level, Name, formatProvider, message, args);
WriteToTargets(logEvent, targetsForLevel);
}
}
private void WriteToTargets(LogLevel level, IFormatProvider formatProvider, [Localizable(false)] string message)
{
var targetsForLevel = GetTargetsForLevel(level);
if (targetsForLevel != null)
{
// please note that this overload calls the overload of LogEventInfo.Create with object[] parameter on purpose -
// to avoid unnecessary string.Format (in case of calling Create(LogLevel, string, IFormatProvider, object))
var logEvent = LogEventInfo.Create(level, Name, formatProvider, message, (object[])null);
WriteToTargets(logEvent, targetsForLevel);
}
}
private void WriteToTargets<T>(LogLevel level, IFormatProvider formatProvider, T value)
{
var targetsForLevel = GetTargetsForLevel(level);
if (targetsForLevel != null)
{
var logEvent = LogEventInfo.Create(level, Name, formatProvider, value);
WriteToTargets(logEvent, targetsForLevel);
}
}
private void WriteToTargets(LogLevel level, Exception ex, [Localizable(false)] string message, object[] args)
{
var targetsForLevel = GetTargetsForLevel(level);
if (targetsForLevel != null)
{
var logEvent = LogEventInfo.Create(level, Name, ex, Factory.DefaultCultureInfo, message, args);
WriteToTargets(logEvent, targetsForLevel);
}
}
private void WriteToTargets(LogLevel level, Exception ex, IFormatProvider formatProvider, [Localizable(false)] string message, object[] args)
{
var targetsForLevel = GetTargetsForLevel(level);
if (targetsForLevel != null)
{
var logEvent = LogEventInfo.Create(level, Name, ex, formatProvider, message, args);
WriteToTargets(logEvent, targetsForLevel);
}
}
private void WriteToTargets([NotNull] LogEventInfo logEvent, [NotNull] TargetWithFilterChain targetsForLevel)
{
LoggerImpl.Write(DefaultLoggerType, targetsForLevel, PrepareLogEventInfo(logEvent), Factory);
}
private void WriteToTargets(Type wrapperType, [NotNull] LogEventInfo logEvent, [NotNull] TargetWithFilterChain targetsForLevel)
{
LoggerImpl.Write(wrapperType ?? DefaultLoggerType, targetsForLevel, PrepareLogEventInfo(logEvent), Factory);
}
internal void SetConfiguration(LoggerConfiguration newConfiguration)
{
_configuration = newConfiguration;
// pre-calculate 'enabled' flags
_isTraceEnabled = IsEnabled(LogLevel.Trace);
_isDebugEnabled = IsEnabled(LogLevel.Debug);
_isInfoEnabled = IsEnabled(LogLevel.Info);
_isWarnEnabled = IsEnabled(LogLevel.Warn);
_isErrorEnabled = IsEnabled(LogLevel.Error);
_isFatalEnabled = IsEnabled(LogLevel.Fatal);
OnLoggerReconfigured(EventArgs.Empty);
}
private TargetWithFilterChain GetTargetsForLevel(LogLevel level)
{
if (ReferenceEquals(_contextLogger, this))
return _configuration.GetTargetsForLevel(level);
else
return _contextLogger.GetTargetsForLevel(level); // Use the LoggerConfiguration of the parent Logger
}
/// <summary>
/// Raises the event when the logger is reconfigured.
/// </summary>
/// <param name="e">Event arguments</param>
protected virtual void OnLoggerReconfigured(EventArgs e)
{
LoggerReconfigured?.Invoke(this, e);
}
}
}