@@ -21,22 +21,35 @@ class ConfigurationReader : IConfigurationReader
21
21
const string LevelSwitchNameRegex = @"^\$[A-Za-z]+[A-Za-z0-9]*$" ;
22
22
const string ConfigSectionHintChar = ">" ;
23
23
24
- readonly IConfigurationSection _configuration ;
24
+ static IConfiguration _configuration ;
25
+
26
+ readonly IConfigurationSection _section ;
25
27
readonly DependencyContext _dependencyContext ;
26
28
readonly IReadOnlyCollection < Assembly > _configurationAssemblies ;
27
29
28
30
#region Constructors
29
31
30
- public ConfigurationReader ( IConfigurationSection configuration , DependencyContext dependencyContext )
32
+ public ConfigurationReader ( IConfiguration configuration , DependencyContext dependencyContext )
31
33
{
32
34
_configuration = configuration ?? throw new ArgumentNullException ( nameof ( configuration ) ) ;
35
+ _section = configuration . GetSection ( ConfigurationLoggerConfigurationExtensions . DefaultSectionName ) ;
33
36
_dependencyContext = dependencyContext ;
34
37
_configurationAssemblies = LoadConfigurationAssemblies ( ) ;
35
38
}
36
39
37
- ConfigurationReader ( IConfigurationSection configuration , IReadOnlyCollection < Assembly > configurationAssemblies , DependencyContext dependencyContext )
40
+ // Generally the initial call should use IConfiguration rather than IConfigurationSection, otherwise
41
+ // IConfiguration parameters in the target methods will not be populated.
42
+ ConfigurationReader ( IConfigurationSection configSection , DependencyContext dependencyContext )
38
43
{
39
- _configuration = configuration ?? throw new ArgumentNullException ( nameof ( configuration ) ) ;
44
+ _section = configSection ?? throw new ArgumentNullException ( nameof ( configSection ) ) ;
45
+ _dependencyContext = dependencyContext ;
46
+ _configurationAssemblies = LoadConfigurationAssemblies ( ) ;
47
+ }
48
+
49
+ // Used internally for processing nested configuration sections -- see GetMethodCalls below.
50
+ ConfigurationReader ( IConfigurationSection configSection , IReadOnlyCollection < Assembly > configurationAssemblies , DependencyContext dependencyContext )
51
+ {
52
+ _section = configSection ?? throw new ArgumentNullException ( nameof ( configSection ) ) ;
40
53
_dependencyContext = dependencyContext ;
41
54
_configurationAssemblies = configurationAssemblies ?? throw new ArgumentNullException ( nameof ( configurationAssemblies ) ) ;
42
55
}
@@ -45,7 +58,6 @@ public ConfigurationReader(IConfigurationSection configuration, DependencyContex
45
58
46
59
#region Configure and related Apply methods
47
60
48
- // Called by LoggerConfiguration in Serilog Core
49
61
public void Configure ( LoggerConfiguration loggerConfiguration )
50
62
{
51
63
var declaredLevelSwitches = ProcessLevelSwitchDeclarations ( ) ;
@@ -58,7 +70,7 @@ public void Configure(LoggerConfiguration loggerConfiguration)
58
70
59
71
IReadOnlyDictionary < string , LoggingLevelSwitch > ProcessLevelSwitchDeclarations ( )
60
72
{
61
- var levelSwitchesDirective = _configuration . GetSection ( "LevelSwitches" ) ;
73
+ var levelSwitchesDirective = _section . GetSection ( "LevelSwitches" ) ;
62
74
var namedSwitches = new Dictionary < string , LoggingLevelSwitch > ( ) ;
63
75
foreach ( var levelSwitchDeclaration in levelSwitchesDirective . GetChildren ( ) )
64
76
{
@@ -86,7 +98,7 @@ IReadOnlyDictionary<string, LoggingLevelSwitch> ProcessLevelSwitchDeclarations()
86
98
87
99
void ApplyMinimumLevel ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
88
100
{
89
- var minimumLevelDirective = _configuration . GetSection ( "MinimumLevel" ) ;
101
+ var minimumLevelDirective = _section . GetSection ( "MinimumLevel" ) ;
90
102
91
103
var defaultMinLevelDirective = minimumLevelDirective . Value != null ? minimumLevelDirective : minimumLevelDirective . GetSection ( "Default" ) ;
92
104
if ( defaultMinLevelDirective . Value != null )
@@ -139,7 +151,7 @@ void ApplyMinimumLevel(IConfigurationSection directive, Action<LoggerMinimumLeve
139
151
140
152
void ApplyFilters ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
141
153
{
142
- var filterDirective = _configuration . GetSection ( "Filter" ) ;
154
+ var filterDirective = _section . GetSection ( "Filter" ) ;
143
155
if ( filterDirective . GetChildren ( ) . Any ( ) )
144
156
{
145
157
var methodCalls = GetMethodCalls ( filterDirective ) ;
@@ -149,7 +161,7 @@ void ApplyFilters(LoggerConfiguration loggerConfiguration, IReadOnlyDictionary<s
149
161
150
162
void ApplySinks ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
151
163
{
152
- var writeToDirective = _configuration . GetSection ( "WriteTo" ) ;
164
+ var writeToDirective = _section . GetSection ( "WriteTo" ) ;
153
165
if ( writeToDirective . GetChildren ( ) . Any ( ) )
154
166
{
155
167
var methodCalls = GetMethodCalls ( writeToDirective ) ;
@@ -159,7 +171,7 @@ void ApplySinks(LoggerConfiguration loggerConfiguration, IReadOnlyDictionary<str
159
171
160
172
void ApplyAuditSinks ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
161
173
{
162
- var auditToDirective = _configuration . GetSection ( "AuditTo" ) ;
174
+ var auditToDirective = _section . GetSection ( "AuditTo" ) ;
163
175
if ( auditToDirective . GetChildren ( ) . Any ( ) )
164
176
{
165
177
var methodCalls = GetMethodCalls ( auditToDirective ) ;
@@ -169,20 +181,20 @@ void ApplyAuditSinks(LoggerConfiguration loggerConfiguration, IReadOnlyDictionar
169
181
170
182
void IConfigurationReader . ApplySinks ( LoggerSinkConfiguration loggerSinkConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
171
183
{
172
- var methodCalls = GetMethodCalls ( _configuration ) ;
184
+ var methodCalls = GetMethodCalls ( _section ) ;
173
185
CallConfigurationMethods ( methodCalls , FindSinkConfigurationMethods ( _configurationAssemblies ) , loggerSinkConfiguration , declaredLevelSwitches ) ;
174
186
}
175
187
176
188
void ApplyEnrichment ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
177
189
{
178
- var enrichDirective = _configuration . GetSection ( "Enrich" ) ;
190
+ var enrichDirective = _section . GetSection ( "Enrich" ) ;
179
191
if ( enrichDirective . GetChildren ( ) . Any ( ) )
180
192
{
181
193
var methodCalls = GetMethodCalls ( enrichDirective ) ;
182
194
CallConfigurationMethods ( methodCalls , FindEventEnricherConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Enrich , declaredLevelSwitches ) ;
183
195
}
184
196
185
- var propertiesDirective = _configuration . GetSection ( "Properties" ) ;
197
+ var propertiesDirective = _section . GetSection ( "Properties" ) ;
186
198
if ( propertiesDirective . GetChildren ( ) . Any ( ) )
187
199
{
188
200
foreach ( var enrichProperyDirective in propertiesDirective . GetChildren ( ) )
@@ -253,7 +265,7 @@ IReadOnlyCollection<Assembly> LoadConfigurationAssemblies()
253
265
{
254
266
var assemblies = new Dictionary < string , Assembly > ( ) ;
255
267
256
- var usingSection = _configuration . GetSection ( "Using" ) ;
268
+ var usingSection = _section . GetSection ( "Using" ) ;
257
269
if ( usingSection . GetChildren ( ) . Any ( ) )
258
270
{
259
271
foreach ( var simpleName in usingSection . GetChildren ( ) . Select ( c => c . Value ) )
@@ -314,6 +326,9 @@ static void CallConfigurationMethods(ILookup<string, Dictionary<string, IConfigu
314
326
let directive = method . Value . FirstOrDefault ( s => s . Key == p . Name )
315
327
select directive . Key == null ? p . DefaultValue : directive . Value . ConvertTo ( p . ParameterType , declaredLevelSwitches ) ) . ToList ( ) ;
316
328
329
+ var parm = methodInfo . GetParameters ( ) . FirstOrDefault ( i => i . ParameterType == typeof ( IConfiguration ) ) ;
330
+ if ( parm != null ) call [ parm . Position - 1 ] = _configuration ;
331
+
317
332
call . Insert ( 0 , receiver ) ;
318
333
319
334
methodInfo . Invoke ( null , call . ToArray ( ) ) ;
0 commit comments