-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
LoggingConfiguration.cs
895 lines (785 loc) · 36.2 KB
/
LoggingConfiguration.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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
//
// Copyright (c) 2004-2019 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.Config
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Threading;
using JetBrains.Annotations;
using NLog.Common;
using NLog.Internal;
using NLog.Layouts;
using NLog.Targets;
using NLog.Targets.Wrappers;
/// <summary>
/// Keeps logging configuration and provides simple API to modify it.
/// </summary>
///<remarks>This class is thread-safe.<c>.ToList()</c> is used for that purpose.</remarks>
public class LoggingConfiguration
{
private readonly IDictionary<string, Target> _targets =
new Dictionary<string, Target>(StringComparer.OrdinalIgnoreCase);
private List<object> _configItems = new List<object>();
/// <summary>
/// Variables defined in xml or in API. name is case case insensitive.
/// </summary>
private readonly ThreadSafeDictionary<string, SimpleLayout> _variables = new ThreadSafeDictionary<string, SimpleLayout>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Gets the factory that will be configured
/// </summary>
public LogFactory LogFactory { get; }
/// <summary>
/// Initializes a new instance of the <see cref="LoggingConfiguration" /> class.
/// </summary>
public LoggingConfiguration()
: this(LogManager.LogFactory)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LoggingConfiguration" /> class.
/// </summary>
public LoggingConfiguration(LogFactory logFactory)
{
LogFactory = logFactory;
LoggingRules = new List<LoggingRule>();
}
/// <summary>
/// Use the old exception log handling of NLog 3.0?
/// </summary>
/// <remarks>This method was marked as obsolete on NLog 4.1 and it may be removed in a future release.</remarks>
[Obsolete("This option will be removed in NLog 5. Marked obsolete on NLog 4.1")]
public bool ExceptionLoggingOldStyle { get; set; }
/// <summary>
/// Gets the variables defined in the configuration.
/// </summary>
public IDictionary<string, SimpleLayout> Variables => _variables;
/// <summary>
/// Gets a collection of named targets specified in the configuration.
/// </summary>
/// <returns>
/// A list of named targets.
/// </returns>
/// <remarks>
/// Unnamed targets (such as those wrapped by other targets) are not returned.
/// </remarks>
public ReadOnlyCollection<Target> ConfiguredNamedTargets => GetAllTargetsThreadSafe().AsReadOnly();
/// <summary>
/// Gets the collection of file names which should be watched for changes by NLog.
/// </summary>
public virtual IEnumerable<string> FileNamesToWatch => ArrayHelper.Empty<string>();
/// <summary>
/// Gets the collection of logging rules.
/// </summary>
public IList<LoggingRule> LoggingRules { get; private set; }
internal List<LoggingRule> GetLoggingRulesThreadSafe() { lock (LoggingRules) return LoggingRules.ToList(); }
private void AddLoggingRulesThreadSafe(LoggingRule rule) { lock (LoggingRules) LoggingRules.Add(rule); }
private bool TryGetTargetThreadSafe(string name, out Target target) { lock (_targets) return _targets.TryGetValue(name, out target); }
private List<Target> GetAllTargetsThreadSafe() { lock (_targets) return _targets.Values.ToList(); }
private Target RemoveTargetThreadSafe(string name)
{
Target target;
lock (_targets)
{
if (_targets.TryGetValue(name, out target))
{
_targets.Remove(name);
}
}
if (target != null)
{
InternalLogger.Debug("Unregistered target {0}: {1}", name, target.GetType().FullName);
}
return target;
}
private void AddTargetThreadSafe(string name, Target target, bool forceOverwrite)
{
if (string.IsNullOrEmpty(name) && !forceOverwrite)
return;
lock (_targets)
{
if (!forceOverwrite && _targets.ContainsKey(name))
return;
_targets[name] = target;
}
if (!string.IsNullOrEmpty(target.Name) && target.Name != name)
{
InternalLogger.Info("Registered target {0}: {1} (Target created with different name: {2})", name, target.GetType().FullName, target.Name);
}
else
{
InternalLogger.Debug("Registered target {0}: {1}", name, target.GetType().FullName);
}
}
/// <summary>
/// Gets or sets the default culture info to use as <see cref="LogEventInfo.FormatProvider"/>.
/// </summary>
/// <value>
/// Specific culture info or null to use <see cref="CultureInfo.CurrentCulture"/>
/// </value>
[CanBeNull]
public CultureInfo DefaultCultureInfo { get; set; }
/// <summary>
/// Gets all targets.
/// </summary>
public ReadOnlyCollection<Target> AllTargets
{
get
{
var configTargets = _configItems.OfType<Target>();
return configTargets.Concat(GetAllTargetsThreadSafe()).Distinct(TargetNameComparer).ToList().AsReadOnly();
}
}
/// <summary>
/// Compare <see cref="Target"/> objects based on their name.
/// </summary>
/// <remarks>This property is use to cache the comparer object.</remarks>
private static readonly IEqualityComparer<Target> TargetNameComparer = new TargetNameEqualityComparer();
/// <summary>
/// Defines methods to support the comparison of <see cref="Target"/> objects for equality based on their name.
/// </summary>
private class TargetNameEqualityComparer : IEqualityComparer<Target>
{
public bool Equals(Target x, Target y)
{
return string.Equals(x.Name, y.Name);
}
public int GetHashCode(Target obj)
{
return (obj.Name != null ? obj.Name.GetHashCode() : 0);
}
}
/// <summary>
/// Registers the specified target object. The name of the target is read from <see cref="Target.Name"/>.
/// </summary>
/// <param name="target">
/// The target object with a non <see langword="null"/> <see cref="Target.Name"/>
/// </param>
/// <exception cref="ArgumentNullException">when <paramref name="target"/> is <see langword="null"/></exception>
public void AddTarget([NotNull] Target target)
{
if (target == null) { throw new ArgumentNullException(nameof(target)); }
if (target.Name == null) { throw new ArgumentNullException(nameof(target) + ".Name cannot be null."); }
AddTargetThreadSafe(target.Name, target, true);
}
/// <summary>
/// Registers the specified target object under a given name.
/// </summary>
/// <param name="name">Name of the target.</param>
/// <param name="target">The target object.</param>
/// <exception cref="ArgumentException">when <paramref name="name"/> is <see langword="null"/></exception>
/// <exception cref="ArgumentNullException">when <paramref name="target"/> is <see langword="null"/></exception>
public void AddTarget(string name, Target target)
{
if (name == null)
{
// TODO: NLog 5 - The ArgumentException should be changed to ArgumentNullException for name parameter.
throw new ArgumentException("Target name cannot be null", nameof(name));
}
if (target == null) { throw new ArgumentNullException(nameof(target)); }
AddTargetThreadSafe(name, target, true);
}
/// <summary>
/// Finds the target with the specified name.
/// </summary>
/// <param name="name">
/// The name of the target to be found.
/// </param>
/// <returns>
/// Found target or <see langword="null"/> when the target is not found.
/// </returns>
public Target FindTargetByName(string name)
{
Target value;
if (!TryGetTargetThreadSafe(name, out value))
{
return null;
}
return value;
}
/// <summary>
/// Finds the target with the specified name and specified type.
/// </summary>
/// <param name="name">
/// The name of the target to be found.
/// </param>
/// <typeparam name="TTarget">Type of the target</typeparam>
/// <returns>
/// Found target or <see langword="null"/> when the target is not found of not of type <typeparamref name="TTarget"/>
/// </returns>
public TTarget FindTargetByName<TTarget>(string name)
where TTarget : Target
{
return FindTargetByName(name) as TTarget;
}
/// <summary>
/// Add a rule with min- and maxLevel.
/// </summary>
/// <param name="minLevel">Minimum log level needed to trigger this rule.</param>
/// <param name="maxLevel">Maximum log level needed to trigger this rule.</param>
/// <param name="targetName">Name of the target to be written when the rule matches.</param>
/// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param>
public void AddRule(LogLevel minLevel, LogLevel maxLevel, string targetName, string loggerNamePattern = "*")
{
var target = FindTargetByName(targetName);
if (target == null)
{
throw new NLogRuntimeException("Target '{0}' not found", targetName);
}
AddRule(minLevel, maxLevel, target, loggerNamePattern, false);
}
/// <summary>
/// Add a rule with min- and maxLevel.
/// </summary>
/// <param name="minLevel">Minimum log level needed to trigger this rule.</param>
/// <param name="maxLevel">Maximum log level needed to trigger this rule.</param>
/// <param name="target">Target to be written to when the rule matches.</param>
/// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param>
public void AddRule(LogLevel minLevel, LogLevel maxLevel, Target target, string loggerNamePattern = "*")
{
if (target == null) { throw new ArgumentNullException(nameof(target)); }
AddRule(minLevel, maxLevel, target, loggerNamePattern, false);
}
/// <summary>
/// Add a rule with min- and maxLevel.
/// </summary>
/// <param name="minLevel">Minimum log level needed to trigger this rule.</param>
/// <param name="maxLevel">Maximum log level needed to trigger this rule.</param>
/// <param name="target">Target to be written to when the rule matches.</param>
/// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param>
/// <param name="final">Gets or sets a value indicating whether to quit processing any further rule when this one matches.</param>
public void AddRule(LogLevel minLevel, LogLevel maxLevel, Target target, string loggerNamePattern, bool final)
{
if (target == null) { throw new ArgumentNullException(nameof(target)); }
AddLoggingRulesThreadSafe(new LoggingRule(loggerNamePattern, minLevel, maxLevel, target) { Final = final });
AddTargetThreadSafe(target.Name, target, false);
}
/// <summary>
/// Add a rule for one loglevel.
/// </summary>
/// <param name="level">log level needed to trigger this rule. </param>
/// <param name="targetName">Name of the target to be written when the rule matches.</param>
/// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param>
public void AddRuleForOneLevel(LogLevel level, string targetName, string loggerNamePattern = "*")
{
var target = FindTargetByName(targetName);
if (target == null)
{
throw new NLogConfigurationException("Target '{0}' not found", targetName);
}
AddRuleForOneLevel(level, target, loggerNamePattern, false);
}
/// <summary>
/// Add a rule for one loglevel.
/// </summary>
/// <param name="level">log level needed to trigger this rule. </param>
/// <param name="target">Target to be written to when the rule matches.</param>
/// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param>
public void AddRuleForOneLevel(LogLevel level, Target target, string loggerNamePattern = "*")
{
if (target == null) { throw new ArgumentNullException(nameof(target)); }
AddRuleForOneLevel(level, target, loggerNamePattern, false);
}
/// <summary>
/// Add a rule for one loglevel.
/// </summary>
/// <param name="level">log level needed to trigger this rule. </param>
/// <param name="target">Target to be written to when the rule matches.</param>
/// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param>
/// <param name="final">Gets or sets a value indicating whether to quit processing any further rule when this one matches.</param>
public void AddRuleForOneLevel(LogLevel level, Target target, string loggerNamePattern, bool final)
{
if (target == null) { throw new ArgumentNullException(nameof(target)); }
var loggingRule = new LoggingRule(loggerNamePattern, target) { Final = final };
loggingRule.EnableLoggingForLevel(level);
AddLoggingRulesThreadSafe(loggingRule);
AddTargetThreadSafe(target.Name, target, false);
}
/// <summary>
/// Add a rule for all loglevels.
/// </summary>
/// <param name="targetName">Name of the target to be written when the rule matches.</param>
/// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param>
public void AddRuleForAllLevels(string targetName, string loggerNamePattern = "*")
{
var target = FindTargetByName(targetName);
if (target == null)
{
throw new NLogRuntimeException("Target '{0}' not found", targetName);
}
AddRuleForAllLevels(target, loggerNamePattern, false);
}
/// <summary>
/// Add a rule for all loglevels.
/// </summary>
/// <param name="target">Target to be written to when the rule matches.</param>
/// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param>
public void AddRuleForAllLevels(Target target, string loggerNamePattern = "*")
{
if (target == null) { throw new ArgumentNullException(nameof(target)); }
AddRuleForAllLevels(target, loggerNamePattern, false);
}
/// <summary>
/// Add a rule for all loglevels.
/// </summary>
/// <param name="target">Target to be written to when the rule matches.</param>
/// <param name="loggerNamePattern">Logger name pattern. It may include the '*' wildcard at the beginning, at the end or at both ends.</param>
/// <param name="final">Gets or sets a value indicating whether to quit processing any further rule when this one matches.</param>
public void AddRuleForAllLevels(Target target, string loggerNamePattern, bool final)
{
if (target == null) { throw new ArgumentNullException(nameof(target)); }
var loggingRule = new LoggingRule(loggerNamePattern, target) { Final = final };
loggingRule.EnableLoggingForLevels(LogLevel.MinLevel, LogLevel.MaxLevel);
AddLoggingRulesThreadSafe(loggingRule);
AddTargetThreadSafe(target.Name, target, false);
}
/// <summary>
/// Finds the logging rule with the specified name.
/// </summary>
/// <param name="ruleName">The name of the logging rule to be found.</param>
/// <returns>Found logging rule or <see langword="null"/> when not found.</returns>
public LoggingRule FindRuleByName(string ruleName)
{
if (ruleName == null)
return null;
var loggingRules = GetLoggingRulesThreadSafe();
for (int i = loggingRules.Count - 1; i >= 0; i--)
{
if (string.Equals(loggingRules[i].RuleName, ruleName, StringComparison.OrdinalIgnoreCase))
{
return loggingRules[i];
}
}
return null;
}
/// <summary>
/// Removes the specified named logging rule.
/// </summary>
/// <param name="ruleName">The name of the logging rule to be removed.</param>
/// <returns>Found one or more logging rule to remove, or <see langword="false"/> when not found.</returns>
public bool RemoveRuleByName(string ruleName)
{
if (ruleName == null)
return false;
HashSet<LoggingRule> removedRules = new HashSet<LoggingRule>();
var loggingRules = GetLoggingRulesThreadSafe();
foreach (var loggingRule in loggingRules)
{
if (string.Equals(loggingRule.RuleName, ruleName, StringComparison.OrdinalIgnoreCase))
{
removedRules.Add(loggingRule);
}
}
if (removedRules.Count > 0)
{
lock (LoggingRules)
{
for (int i = LoggingRules.Count - 1; i >= 0; i--)
{
if (removedRules.Contains(LoggingRules[i]))
{
LoggingRules.RemoveAt(i);
}
}
}
}
return removedRules.Count > 0;
}
/// <summary>
/// Called by LogManager when one of the log configuration files changes.
/// </summary>
/// <returns>
/// A new instance of <see cref="LoggingConfiguration"/> that represents the updated configuration.
/// </returns>
public virtual LoggingConfiguration Reload()
{
return this;
}
internal LoggingConfiguration ReloadNewConfig()
{
var newConfig = Reload();
if (newConfig != null)
{
//problem: XmlLoggingConfiguration.Initialize eats exception with invalid XML. ALso XmlLoggingConfiguration.Reload never returns null.
//therefor we check the InitializeSucceeded property.
if (newConfig is XmlLoggingConfiguration xmlConfig && xmlConfig.InitializeSucceeded != true)
{
InternalLogger.Warn("NLog Config Reload() failed. Invalid XML?");
return null;
}
var logFactory = LogFactory ?? LogManager.LogFactory;
if (logFactory.KeepVariablesOnReload)
{
var currentConfig = logFactory._config ?? this;
if (!ReferenceEquals(newConfig, currentConfig))
{
newConfig.CopyVariables(currentConfig.Variables);
}
}
}
return newConfig;
}
/// <summary>
/// Removes the specified named target.
/// </summary>
/// <param name="name">Name of the target.</param>
public void RemoveTarget(string name)
{
HashSet<Target> removedTargets = new HashSet<Target>();
Target removedTarget = RemoveTargetThreadSafe(name);
if (removedTarget != null)
{
removedTargets.Add(removedTarget);
}
if (!string.IsNullOrEmpty(name) || removedTarget != null)
{
CleanupRulesForRemovedTarget(name, removedTarget, removedTargets);
}
if (removedTargets.Count > 0)
{
ValidateConfig(); // Refresh internal list of configurable items (_configItems)
// Refresh active logger-objects, so they stop using the removed target
// - Can be called even if no LoggingConfiguration is loaded (will not trigger a config load)
if (LogFactory != null)
{
LogFactory.ReconfigExistingLoggers();
}
else
{
LogManager.ReconfigExistingLoggers();
}
// Perform flush and close after having stopped logger-objects from using the target
ManualResetEvent flushCompleted = new ManualResetEvent(false);
foreach (var target in removedTargets)
{
flushCompleted.Reset();
target.Flush((ex) => flushCompleted.Set());
flushCompleted.WaitOne(TimeSpan.FromSeconds(15));
target.Close();
}
}
}
private void CleanupRulesForRemovedTarget(string name, Target removedTarget, HashSet<Target> removedTargets)
{
var loggingRules = GetLoggingRulesThreadSafe();
foreach (var rule in loggingRules)
{
var targetList = rule.GetTargetsThreadSafe();
foreach (var target in targetList)
{
if (ReferenceEquals(removedTarget, target) || (!string.IsNullOrEmpty(name) && target.Name == name))
{
removedTargets.Add(target);
rule.RemoveTargetThreadSafe(target);
}
}
}
}
/// <summary>
/// Installs target-specific objects on current system.
/// </summary>
/// <param name="installationContext">The installation context.</param>
/// <remarks>
/// Installation typically runs with administrative permissions.
/// </remarks>
public void Install(InstallationContext installationContext)
{
if (installationContext == null)
{
throw new ArgumentNullException(nameof(installationContext));
}
InitializeAll();
var configItemsList = GetInstallableItems();
foreach (IInstallable installable in configItemsList)
{
installationContext.Info("Installing '{0}'", installable);
try
{
installable.Install(installationContext);
installationContext.Info("Finished installing '{0}'.", installable);
}
catch (Exception exception)
{
InternalLogger.Error(exception, "Install of '{0}' failed.", installable);
if (exception.MustBeRethrownImmediately() || installationContext.ThrowExceptions)
{
throw;
}
installationContext.Error("Install of '{0}' failed: {1}.", installable, exception);
}
}
}
/// <summary>
/// Uninstalls target-specific objects from current system.
/// </summary>
/// <param name="installationContext">The installation context.</param>
/// <remarks>
/// Uninstallation typically runs with administrative permissions.
/// </remarks>
public void Uninstall(InstallationContext installationContext)
{
if (installationContext == null)
{
throw new ArgumentNullException(nameof(installationContext));
}
InitializeAll();
var configItemsList = GetInstallableItems();
foreach (IInstallable installable in configItemsList)
{
installationContext.Info("Uninstalling '{0}'", installable);
try
{
installable.Uninstall(installationContext);
installationContext.Info("Finished uninstalling '{0}'.", installable);
}
catch (Exception exception)
{
InternalLogger.Error(exception, "Uninstall of '{0}' failed.", installable);
if (exception.MustBeRethrownImmediately())
{
throw;
}
installationContext.Error("Uninstall of '{0}' failed: {1}.", installable, exception);
}
}
}
/// <summary>
/// Closes all targets and releases any unmanaged resources.
/// </summary>
internal void Close()
{
InternalLogger.Debug("Closing logging configuration...");
var supportsInitializesList = GetSupportsInitializes();
foreach (ISupportsInitialize initialize in supportsInitializesList)
{
InternalLogger.Trace("Closing {0}", initialize);
try
{
initialize.Close();
}
catch (Exception exception)
{
InternalLogger.Warn(exception, "Exception while closing.");
if (exception.MustBeRethrown())
{
throw;
}
}
}
InternalLogger.Debug("Finished closing logging configuration.");
}
/// <summary>
/// Log to the internal (NLog) logger the information about the <see cref="Target"/> and <see
/// cref="LoggingRule"/> associated with this <see cref="LoggingConfiguration"/> instance.
/// </summary>
/// <remarks>
/// The information are only recorded in the internal logger if Debug level is enabled, otherwise nothing is
/// recorded.
/// </remarks>
internal void Dump()
{
if (!InternalLogger.IsDebugEnabled)
{
return;
}
InternalLogger.Debug("--- NLog configuration dump ---");
InternalLogger.Debug("Targets:");
var targetList = GetAllTargetsThreadSafe();
foreach (Target target in targetList)
{
InternalLogger.Debug("{0}", target);
}
InternalLogger.Debug("Rules:");
foreach (LoggingRule rule in GetLoggingRulesThreadSafe())
{
InternalLogger.Debug("{0}", rule);
}
InternalLogger.Debug("--- End of NLog configuration dump ---");
}
/// <summary>
/// Flushes any pending log messages on all appenders.
/// </summary>
/// <param name="asyncContinuation">The asynchronous continuation.</param>
internal void FlushAllTargets(AsyncContinuation asyncContinuation)
{
InternalLogger.Trace("Flushing all targets...");
var uniqueTargets = new List<Target>();
foreach (var rule in GetLoggingRulesThreadSafe())
{
var targetList = rule.GetTargetsThreadSafe();
foreach (var target in targetList)
{
if (!uniqueTargets.Contains(target))
{
uniqueTargets.Add(target);
}
}
}
AsyncHelpers.ForEachItemInParallel(uniqueTargets, asyncContinuation, (target, cont) => target.Flush(cont));
}
/// <summary>
/// Validates the configuration.
/// </summary>
internal void ValidateConfig()
{
var roots = new List<object>();
foreach (LoggingRule rule in GetLoggingRulesThreadSafe())
{
roots.Add(rule);
}
var targetList = GetAllTargetsThreadSafe();
foreach (Target target in targetList)
{
roots.Add(target);
}
_configItems = ObjectGraphScanner.FindReachableObjects<object>(true, roots.ToArray());
// initialize all config items starting from most nested first
// so that whenever the container is initialized its children have already been
InternalLogger.Info("Found {0} configuration items", _configItems.Count);
foreach (object o in _configItems)
{
PropertyHelper.CheckRequiredParameters(o);
}
}
internal void InitializeAll()
{
ValidateConfig();
var supportsInitializes = GetSupportsInitializes(true);
foreach (ISupportsInitialize initialize in supportsInitializes)
{
InternalLogger.Trace("Initializing {0}", initialize);
try
{
initialize.Initialize(this);
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
{
throw;
}
if (LogManager.ThrowExceptions)
{
throw new NLogConfigurationException($"Error during initialization of {initialize}", exception);
}
}
}
}
private List<IInstallable> GetInstallableItems()
{
return _configItems.OfType<IInstallable>().ToList();
}
private List<ISupportsInitialize> GetSupportsInitializes(bool reverse = false)
{
var items = _configItems.OfType<ISupportsInitialize>();
if (reverse)
{
items = items.Reverse();
}
return items.ToList();
}
/// <summary>
/// Copies all variables from provided dictionary into current configuration variables.
/// </summary>
/// <param name="masterVariables">Master variables dictionary</param>
internal void CopyVariables(IDictionary<string, SimpleLayout> masterVariables)
{
_variables.CopyFrom(masterVariables);
}
/// <summary>
/// Replace a simple variable with a value. The orginal value is removed and thus we cannot redo this in a later stage.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[NotNull]
internal string ExpandSimpleVariables(string input)
{
string output = input;
if (Variables.Count > 0 && output?.IndexOf("${") >= 0)
{
// TODO - make this case-insensitive, will probably require a different approach
var variables = Variables.ToList();
foreach (var kvp in variables)
{
var layout = kvp.Value;
//this value is set from xml and that's a string. Because of that, we can use SimpleLayout here.
if (layout != null) output = output.Replace(string.Concat("${", kvp.Key, "}"), layout.OriginalText);
}
}
return output ?? string.Empty;
}
/// <summary>
/// Checks whether unused targets exist. If found any, just write an internal log at Warn level.
/// <remarks>If initializing not started or failed, then checking process will be canceled</remarks>
/// </summary>
internal void CheckUnusedTargets()
{
ReadOnlyCollection<Target> configuredNamedTargets = ConfiguredNamedTargets; //assign to variable because `ConfiguredNamedTargets` computes a new list every time.
InternalLogger.Debug("Unused target checking is started... Rule Count: {0}, Target Count: {1}", LoggingRules.Count, configuredNamedTargets.Count);
var targetNamesAtRules = new HashSet<string>(GetLoggingRulesThreadSafe().SelectMany(r => r.Targets).Select(t => t.Name));
var wrappedTargets = configuredNamedTargets.OfType<WrapperTargetBase>().ToLookup(wt => wt.WrappedTarget, wt => wt);
var compoundTargets = configuredNamedTargets.OfType<CompoundTargetBase>().SelectMany(wt => wt.Targets.Select(t => new KeyValuePair<Target, Target>(t, wt))).ToLookup(p => p.Key, p => p.Value);
bool IsUnusedInList<T>(Target target1, ILookup<Target, T> targets)
where T:Target
{
if (targets.Contains(target1))
{
foreach (var wrapperTarget in targets[target1])
{
if (targetNamesAtRules.Contains(wrapperTarget.Name))
return false;
if (wrappedTargets.Contains(wrapperTarget))
return false;
if (compoundTargets.Contains(wrapperTarget))
return false;
}
}
return true;
}
int unusedCount = configuredNamedTargets.Count((target) =>
{
if (targetNamesAtRules.Contains(target.Name))
return false;
if (!IsUnusedInList(target, wrappedTargets))
return false;
if (!IsUnusedInList(target, compoundTargets))
return false;
InternalLogger.Warn("Unused target detected. Add a rule for this target to the configuration. TargetName: {0}", target.Name);
return true;
});
InternalLogger.Debug("Unused target checking is completed. Total Rule Count: {0}, Total Target Count: {1}, Unused Target Count: {2}", LoggingRules.Count, configuredNamedTargets.Count, unusedCount);
}
}
}