@@ -19,46 +19,40 @@ class ConfigurationReader : IConfigurationReader
19
19
{
20
20
const string LevelSwitchNameRegex = @"^\$[A-Za-z]+[A-Za-z0-9]*$" ;
21
21
22
- readonly IConfiguration _configuration ;
23
-
24
22
readonly IConfigurationSection _section ;
25
- readonly AssemblyFinder _assemblyFinder ;
26
23
readonly IReadOnlyCollection < Assembly > _configurationAssemblies ;
24
+ readonly ResolutionContext _resolutionContext ;
27
25
28
26
public ConfigurationReader ( IConfigurationSection configSection , AssemblyFinder assemblyFinder , IConfiguration configuration = null )
29
27
{
30
28
_section = configSection ?? throw new ArgumentNullException ( nameof ( configSection ) ) ;
31
- _assemblyFinder = assemblyFinder ?? throw new ArgumentNullException ( nameof ( assemblyFinder ) ) ;
32
- _configuration = configuration ;
33
- _configurationAssemblies = LoadConfigurationAssemblies ( _section , _assemblyFinder ) ;
29
+ _configurationAssemblies = LoadConfigurationAssemblies ( _section , assemblyFinder ) ;
30
+ _resolutionContext = new ResolutionContext ( configuration ) ;
34
31
}
35
32
36
33
// Used internally for processing nested configuration sections -- see GetMethodCalls below.
37
- internal ConfigurationReader ( IConfigurationSection configSection , IReadOnlyCollection < Assembly > configurationAssemblies , AssemblyFinder assemblyFinder , SettingValueResolver valueResolver )
34
+ internal ConfigurationReader ( IConfigurationSection configSection , IReadOnlyCollection < Assembly > configurationAssemblies , ResolutionContext resolutionContext )
38
35
{
39
36
_section = configSection ?? throw new ArgumentNullException ( nameof ( configSection ) ) ;
40
37
_configurationAssemblies = configurationAssemblies ?? throw new ArgumentNullException ( nameof ( configurationAssemblies ) ) ;
41
- _assemblyFinder = assemblyFinder ?? throw new ArgumentNullException ( nameof ( assemblyFinder ) ) ;
42
- _configuration = valueResolver . AppConfiguration ;
38
+ _resolutionContext = resolutionContext ?? throw new ArgumentNullException ( nameof ( resolutionContext ) ) ;
43
39
}
44
40
45
41
public void Configure ( LoggerConfiguration loggerConfiguration )
46
42
{
47
- var declaredLevelSwitches = ProcessLevelSwitchDeclarations ( ) ;
48
- var settingsValueResolver = new SettingValueResolver ( declaredLevelSwitches , _configuration ) ;
49
-
50
- ApplyMinimumLevel ( loggerConfiguration , settingsValueResolver ) ;
51
- ApplyEnrichment ( loggerConfiguration , settingsValueResolver ) ;
52
- ApplyFilters ( loggerConfiguration , settingsValueResolver ) ;
53
- ApplyDestructuring ( loggerConfiguration , settingsValueResolver ) ;
54
- ApplySinks ( loggerConfiguration , settingsValueResolver ) ;
55
- ApplyAuditSinks ( loggerConfiguration , settingsValueResolver ) ;
43
+ ProcessLevelSwitchDeclarations ( ) ;
44
+
45
+ ApplyMinimumLevel ( loggerConfiguration ) ;
46
+ ApplyEnrichment ( loggerConfiguration ) ;
47
+ ApplyFilters ( loggerConfiguration ) ;
48
+ ApplyDestructuring ( loggerConfiguration ) ;
49
+ ApplySinks ( loggerConfiguration ) ;
50
+ ApplyAuditSinks ( loggerConfiguration ) ;
56
51
}
57
52
58
- IReadOnlyDictionary < string , LoggingLevelSwitch > ProcessLevelSwitchDeclarations ( )
53
+ void ProcessLevelSwitchDeclarations ( )
59
54
{
60
55
var levelSwitchesDirective = _section . GetSection ( "LevelSwitches" ) ;
61
- var namedSwitches = new Dictionary < string , LoggingLevelSwitch > ( ) ;
62
56
foreach ( var levelSwitchDeclaration in levelSwitchesDirective . GetChildren ( ) )
63
57
{
64
58
var switchName = levelSwitchDeclaration . Key ;
@@ -78,12 +72,12 @@ IReadOnlyDictionary<string, LoggingLevelSwitch> ProcessLevelSwitchDeclarations()
78
72
var initialLevel = ParseLogEventLevel ( switchInitialLevel ) ;
79
73
newSwitch = new LoggingLevelSwitch ( initialLevel ) ;
80
74
}
81
- namedSwitches . Add ( switchName , newSwitch ) ;
75
+ // make them available later on when resolving argument values
76
+ _resolutionContext . AddLevelSwitch ( switchName , newSwitch ) ;
82
77
}
83
- return namedSwitches ;
84
78
}
85
79
86
- void ApplyMinimumLevel ( LoggerConfiguration loggerConfiguration , SettingValueResolver valueResolver )
80
+ void ApplyMinimumLevel ( LoggerConfiguration loggerConfiguration )
87
81
{
88
82
var minimumLevelDirective = _section . GetSection ( "MinimumLevel" ) ;
89
83
@@ -96,7 +90,7 @@ void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration, SettingValueReso
96
90
var minLevelControlledByDirective = minimumLevelDirective . GetSection ( "ControlledBy" ) ;
97
91
if ( minLevelControlledByDirective . Value != null )
98
92
{
99
- var globalMinimumLevelSwitch = valueResolver . LookUpSwitchByName ( minLevelControlledByDirective . Value ) ;
93
+ var globalMinimumLevelSwitch = _resolutionContext . LookUpSwitchByName ( minLevelControlledByDirective . Value ) ;
100
94
// not calling ApplyMinimumLevel local function because here we have a reference to a LogLevelSwitch already
101
95
loggerConfiguration . MinimumLevel . ControlledBy ( globalMinimumLevelSwitch ) ;
102
96
}
@@ -111,7 +105,7 @@ void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration, SettingValueReso
111
105
}
112
106
else
113
107
{
114
- var overrideSwitch = valueResolver . LookUpSwitchByName ( overridenLevelOrSwitch ) ;
108
+ var overrideSwitch = _resolutionContext . LookUpSwitchByName ( overridenLevelOrSwitch ) ;
115
109
// not calling ApplyMinimumLevel local function because here we have a reference to a LogLevelSwitch already
116
110
loggerConfiguration . MinimumLevel . Override ( overridePrefix , overrideSwitch ) ;
117
111
}
@@ -136,59 +130,59 @@ void ApplyMinimumLevel(IConfigurationSection directive, Action<LoggerMinimumLeve
136
130
}
137
131
}
138
132
139
- void ApplyFilters ( LoggerConfiguration loggerConfiguration , SettingValueResolver valueResolver )
133
+ void ApplyFilters ( LoggerConfiguration loggerConfiguration )
140
134
{
141
135
var filterDirective = _section . GetSection ( "Filter" ) ;
142
136
if ( filterDirective . GetChildren ( ) . Any ( ) )
143
137
{
144
138
var methodCalls = GetMethodCalls ( filterDirective ) ;
145
- CallConfigurationMethods ( methodCalls , FindFilterConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Filter , valueResolver ) ;
139
+ CallConfigurationMethods ( methodCalls , FindFilterConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Filter ) ;
146
140
}
147
141
}
148
142
149
- void ApplyDestructuring ( LoggerConfiguration loggerConfiguration , SettingValueResolver valueResolver )
143
+ void ApplyDestructuring ( LoggerConfiguration loggerConfiguration )
150
144
{
151
145
var destructureDirective = _section . GetSection ( "Destructure" ) ;
152
146
if ( destructureDirective . GetChildren ( ) . Any ( ) )
153
147
{
154
148
var methodCalls = GetMethodCalls ( destructureDirective ) ;
155
- CallConfigurationMethods ( methodCalls , FindDestructureConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Destructure , valueResolver ) ;
149
+ CallConfigurationMethods ( methodCalls , FindDestructureConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Destructure ) ;
156
150
}
157
151
}
158
152
159
- void ApplySinks ( LoggerConfiguration loggerConfiguration , SettingValueResolver valueResolver )
153
+ void ApplySinks ( LoggerConfiguration loggerConfiguration )
160
154
{
161
155
var writeToDirective = _section . GetSection ( "WriteTo" ) ;
162
156
if ( writeToDirective . GetChildren ( ) . Any ( ) )
163
157
{
164
158
var methodCalls = GetMethodCalls ( writeToDirective ) ;
165
- CallConfigurationMethods ( methodCalls , FindSinkConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . WriteTo , valueResolver ) ;
159
+ CallConfigurationMethods ( methodCalls , FindSinkConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . WriteTo ) ;
166
160
}
167
161
}
168
162
169
- void ApplyAuditSinks ( LoggerConfiguration loggerConfiguration , SettingValueResolver valueResolver )
163
+ void ApplyAuditSinks ( LoggerConfiguration loggerConfiguration )
170
164
{
171
165
var auditToDirective = _section . GetSection ( "AuditTo" ) ;
172
166
if ( auditToDirective . GetChildren ( ) . Any ( ) )
173
167
{
174
168
var methodCalls = GetMethodCalls ( auditToDirective ) ;
175
- CallConfigurationMethods ( methodCalls , FindAuditSinkConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . AuditTo , valueResolver ) ;
169
+ CallConfigurationMethods ( methodCalls , FindAuditSinkConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . AuditTo ) ;
176
170
}
177
171
}
178
172
179
- void IConfigurationReader . ApplySinks ( LoggerSinkConfiguration loggerSinkConfiguration , SettingValueResolver valueResolver )
173
+ void IConfigurationReader . ApplySinks ( LoggerSinkConfiguration loggerSinkConfiguration )
180
174
{
181
175
var methodCalls = GetMethodCalls ( _section ) ;
182
- CallConfigurationMethods ( methodCalls , FindSinkConfigurationMethods ( _configurationAssemblies ) , loggerSinkConfiguration , valueResolver ) ;
176
+ CallConfigurationMethods ( methodCalls , FindSinkConfigurationMethods ( _configurationAssemblies ) , loggerSinkConfiguration ) ;
183
177
}
184
178
185
- void ApplyEnrichment ( LoggerConfiguration loggerConfiguration , SettingValueResolver valueResolver )
179
+ void ApplyEnrichment ( LoggerConfiguration loggerConfiguration )
186
180
{
187
181
var enrichDirective = _section . GetSection ( "Enrich" ) ;
188
182
if ( enrichDirective . GetChildren ( ) . Any ( ) )
189
183
{
190
184
var methodCalls = GetMethodCalls ( enrichDirective ) ;
191
- CallConfigurationMethods ( methodCalls , FindEventEnricherConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Enrich , valueResolver ) ;
185
+ CallConfigurationMethods ( methodCalls , FindEventEnricherConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Enrich ) ;
192
186
}
193
187
194
188
var propertiesDirective = _section . GetSection ( "Properties" ) ;
@@ -243,7 +237,7 @@ IConfigurationArgumentValue GetArgumentValue(IConfigurationSection argumentSecti
243
237
}
244
238
else
245
239
{
246
- argumentValue = new ObjectArgumentValue ( argumentSection , _configurationAssemblies , _assemblyFinder ) ;
240
+ argumentValue = new ObjectArgumentValue ( argumentSection , _configurationAssemblies ) ;
247
241
}
248
242
249
243
return argumentValue ;
@@ -288,7 +282,7 @@ static IReadOnlyCollection<Assembly> LoadConfigurationAssemblies(IConfigurationS
288
282
return assemblies . Values . ToList ( ) . AsReadOnly ( ) ;
289
283
}
290
284
291
- static void CallConfigurationMethods ( ILookup < string , Dictionary < string , IConfigurationArgumentValue > > methods , IList < MethodInfo > configurationMethods , object receiver , SettingValueResolver valueResolver )
285
+ void CallConfigurationMethods ( ILookup < string , Dictionary < string , IConfigurationArgumentValue > > methods , IList < MethodInfo > configurationMethods , object receiver )
292
286
{
293
287
foreach ( var method in methods . SelectMany ( g => g . Select ( x => new { g . Key , Value = x } ) ) )
294
288
{
@@ -299,8 +293,8 @@ static void CallConfigurationMethods(ILookup<string, Dictionary<string, IConfigu
299
293
var call = ( from p in methodInfo . GetParameters ( ) . Skip ( 1 )
300
294
let directive = method . Value . FirstOrDefault ( s => ParameterNameMatches ( p . Name , s . Key ) )
301
295
select directive . Key == null
302
- ? GetImplicitValueForNotSpecifiedKey ( p , valueResolver , methodInfo )
303
- : directive . Value . ConvertTo ( p . ParameterType , valueResolver ) ) . ToList ( ) ;
296
+ ? GetImplicitValueForNotSpecifiedKey ( p , methodInfo )
297
+ : directive . Value . ConvertTo ( p . ParameterType , _resolutionContext ) ) . ToList ( ) ;
304
298
305
299
call . Insert ( 0 , receiver ) ;
306
300
methodInfo . Invoke ( null , call . ToArray ( ) ) ;
@@ -315,7 +309,7 @@ static bool HasImplicitValueWhenNotSpecified(ParameterInfo paramInfo)
315
309
|| paramInfo . ParameterType == typeof ( IConfiguration ) ;
316
310
}
317
311
318
- static object GetImplicitValueForNotSpecifiedKey ( ParameterInfo parameter , SettingValueResolver valueResolver , MethodInfo methodToInvoke )
312
+ object GetImplicitValueForNotSpecifiedKey ( ParameterInfo parameter , MethodInfo methodToInvoke )
319
313
{
320
314
if ( ! HasImplicitValueWhenNotSpecified ( parameter ) )
321
315
{
@@ -325,13 +319,16 @@ static object GetImplicitValueForNotSpecifiedKey(ParameterInfo parameter, Settin
325
319
326
320
if ( parameter . ParameterType == typeof ( IConfiguration ) )
327
321
{
322
+ if ( _resolutionContext . HasAppConfiguration )
323
+ {
324
+ return _resolutionContext . AppConfiguration ;
325
+ }
328
326
if ( parameter . HasDefaultValue )
329
327
{
330
- return valueResolver . AppConfiguration ?? parameter . DefaultValue ;
328
+ return parameter . DefaultValue ;
331
329
}
332
330
333
- return valueResolver . AppConfiguration
334
- ?? throw new InvalidOperationException ( "Trying to invoke a configuration method accepting a `IConfiguration` argument. " +
331
+ throw new InvalidOperationException ( "Trying to invoke a configuration method accepting a `IConfiguration` argument. " +
335
332
$ "This is not supported when only a `IConfigSection` has been provided. (method '{ methodToInvoke } ')") ;
336
333
}
337
334
0 commit comments