-
Notifications
You must be signed in to change notification settings - Fork 794
/
LoggerEnrichmentConfiguration.cs
190 lines (165 loc) · 9.99 KB
/
LoggerEnrichmentConfiguration.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
// Copyright 2013-2015 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace Serilog.Configuration;
/// <summary>
/// Controls enrichment configuration.
/// </summary>
public class LoggerEnrichmentConfiguration
{
readonly LoggerConfiguration _loggerConfiguration;
readonly Action<ILogEventEnricher> _addEnricher;
internal LoggerEnrichmentConfiguration(
LoggerConfiguration loggerConfiguration,
Action<ILogEventEnricher> addEnricher)
{
_loggerConfiguration = loggerConfiguration ?? throw new ArgumentNullException(nameof(loggerConfiguration));
_addEnricher = addEnricher ?? throw new ArgumentNullException(nameof(addEnricher));
}
/// <summary>
/// Specifies one or more enrichers that may add properties dynamically to
/// log events.
/// </summary>
/// <param name="enrichers">Enrichers to apply to all events passing through
/// the logger.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <exception cref="ArgumentNullException">When <paramref name="enrichers"/> is <code>null</code></exception>
/// <exception cref="ArgumentException">When any element of <paramref name="enrichers"/> is <code>null</code></exception>
public LoggerConfiguration With(params ILogEventEnricher[] enrichers)
{
if (enrichers == null) throw new ArgumentNullException(nameof(enrichers));
foreach (var logEventEnricher in enrichers)
{
if (logEventEnricher == null) throw new ArgumentException("Null enricher is not allowed.");
_addEnricher(logEventEnricher);
}
return _loggerConfiguration;
}
/// <summary>
/// Specifies an enricher that may add properties dynamically to
/// log events.
/// </summary>
/// <typeparam name="TEnricher">Enricher type to apply to all events passing through
/// the logger.</typeparam>
/// <returns>Configuration object allowing method chaining.</returns>
public LoggerConfiguration With<TEnricher>()
where TEnricher : ILogEventEnricher, new()
{
return With(new TEnricher());
}
/// <summary>
/// Include the specified property value in all events logged to the logger.
/// </summary>
/// <param name="name">The name of the property to add.</param>
/// <param name="value">The property value to add.</param>
/// <param name="destructureObjects">If true, objects of unknown type will be logged as structures; otherwise they will be converted using <see cref="object.ToString"/>.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public LoggerConfiguration WithProperty(string name, object value, bool destructureObjects = false)
{
return With(new PropertyEnricher(name, value, destructureObjects));
}
/// <summary>
/// Enrich log events with properties from <see cref="Context.LogContext"/>.
/// </summary>
/// <returns>Configuration object allowing method chaining.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <returns>Configuration object allowing method chaining.</returns>
public LoggerConfiguration FromLogContext() => With<LogContextEnricher>();
/// <summary>
/// Apply an enricher only when <paramref name="condition"/> evaluates to <c>true</c>.
/// </summary>
/// <param name="condition">A predicate that evaluates to <c>true</c> when the supplied <see cref="LogEvent"/>
/// should be enriched.</param>
/// <param name="configureEnricher">An action that configures the wrapped enricher.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <exception cref="ArgumentNullException">When <paramref name="condition"/> is <code>null</code></exception>
/// <exception cref="ArgumentNullException">When <paramref name="configureEnricher"/> is <code>null</code></exception>
public LoggerConfiguration When(Func<LogEvent, bool> condition, Action<LoggerEnrichmentConfiguration> configureEnricher)
{
if (condition == null) throw new ArgumentNullException(nameof(condition));
if (configureEnricher == null) throw new ArgumentNullException(nameof(configureEnricher));
return Wrap(this, e => new ConditionalEnricher(e, condition), configureEnricher);
}
/// <summary>
/// Apply an enricher only to events with a <see cref="LogEventLevel"/> greater than or equal to <paramref name="enrichFromLevel"/>.
/// </summary>
/// <param name="enrichFromLevel">The level from which the enricher will be applied.</param>
/// <param name="configureEnricher">An action that configures the wrapped enricher.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <remarks>This method permits additional information to be attached to e.g. warnings and errors, that might be too expensive
/// to collect or store at lower levels.</remarks>
/// <exception cref="ArgumentNullException">When <paramref name="configureEnricher"/> is <code>null</code></exception>
public LoggerConfiguration AtLevel(LogEventLevel enrichFromLevel, Action<LoggerEnrichmentConfiguration> configureEnricher)
{
if (configureEnricher == null) throw new ArgumentNullException(nameof(configureEnricher));
return Wrap(this, e => new ConditionalEnricher(e, le => le.Level >= enrichFromLevel), configureEnricher);
}
/// <summary>
/// Apply an enricher only to events with a <see cref="LogEventLevel"/> greater than or equal to the level specified by <paramref name="levelSwitch"/>.
/// </summary>
/// <param name="levelSwitch">A <see cref="LoggingLevelSwitch"/> that specifies the level from which the enricher will be applied.</param>
/// <param name="configureEnricher">An action that configures the wrapped enricher.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <remarks>This method permits additional information to be attached to e.g. warnings and errors, that might be too expensive
/// to collect or store at lower levels.</remarks>
/// <exception cref="ArgumentNullException">When <paramref name="configureEnricher"/> is <code>null</code></exception>
public LoggerConfiguration AtLevel(LoggingLevelSwitch levelSwitch, Action<LoggerEnrichmentConfiguration> configureEnricher)
{
if (configureEnricher == null) throw new ArgumentNullException(nameof(configureEnricher));
return Wrap(this, e => new ConditionalEnricher(e, le => le.Level >= levelSwitch.MinimumLevel), configureEnricher);
}
/// <summary>
/// Helper method for wrapping sinks.
/// </summary>
/// <param name="loggerEnrichmentConfiguration">The parent enrichment configuration.</param>
/// <param name="wrapEnricher">A function that allows for wrapping <see cref="ILogEventEnricher"/>s
/// added in <paramref name="configureWrappedEnricher"/>.</param>
/// <param name="configureWrappedEnricher">An action that configures enrichers to be wrapped in <paramref name="wrapEnricher"/>.</param>
/// <returns>Configuration object allowing method chaining.</returns>
/// <exception cref="ArgumentNullException">When <paramref name="loggerEnrichmentConfiguration"/> is <code>null</code></exception>
/// <exception cref="ArgumentNullException">When <paramref name="wrapEnricher"/> is <code>null</code></exception>
/// <exception cref="ArgumentNullException">When <paramref name="configureWrappedEnricher"/> is <code>null</code></exception>
public static LoggerConfiguration Wrap(
LoggerEnrichmentConfiguration loggerEnrichmentConfiguration,
Func<ILogEventEnricher, ILogEventEnricher> wrapEnricher,
Action<LoggerEnrichmentConfiguration> configureWrappedEnricher)
{
if (loggerEnrichmentConfiguration == null) throw new ArgumentNullException(nameof(loggerEnrichmentConfiguration));
if (wrapEnricher == null) throw new ArgumentNullException(nameof(wrapEnricher));
if (configureWrappedEnricher == null) throw new ArgumentNullException(nameof(configureWrappedEnricher));
var enrichersToWrap = new List<ILogEventEnricher>();
var capturingConfiguration = new LoggerConfiguration();
var capturingLoggerEnrichmentConfiguration = new LoggerEnrichmentConfiguration(
capturingConfiguration,
enrichersToWrap.Add);
// `Enrich.With()` will return the capturing configuration; this ensures chained `Enrich` gets back
// to the capturing enrichment configuration, enabling `Enrich.WithX().Enrich.WithY()`.
capturingConfiguration.Enrich = capturingLoggerEnrichmentConfiguration;
configureWrappedEnricher(capturingLoggerEnrichmentConfiguration);
if (enrichersToWrap.Count == 0)
return loggerEnrichmentConfiguration._loggerConfiguration;
var enclosed = enrichersToWrap.Count == 1 ?
enrichersToWrap.Single() :
// Enrichment failures are not considered blocking for auditing purposes.
new SafeAggregateEnricher(enrichersToWrap);
var wrappedEnricher = wrapEnricher(enclosed);
// ReSharper disable once SuspiciousTypeConversion.Global
if (!(wrappedEnricher is IDisposable))
{
SelfLog.WriteLine("Wrapping enricher {0} does not implement IDisposable; to ensure " +
"wrapped enrichers are properly disposed, wrappers should dispose " +
"their wrapped contents", wrappedEnricher);
}
return loggerEnrichmentConfiguration.With(wrappedEnricher);
}
}