@@ -19,7 +19,7 @@ class ConfigurationReader : IConfigurationReader
19
19
{
20
20
const string LevelSwitchNameRegex = @"^\$[A-Za-z]+[A-Za-z0-9]*$" ;
21
21
22
- static IConfiguration _configuration ;
22
+ readonly IConfiguration _configuration ;
23
23
24
24
readonly IConfigurationSection _section ;
25
25
readonly DependencyContext _dependencyContext ;
@@ -43,22 +43,25 @@ public ConfigurationReader(IConfigurationSection configSection, DependencyContex
43
43
}
44
44
45
45
// Used internally for processing nested configuration sections -- see GetMethodCalls below.
46
- internal ConfigurationReader ( IConfigurationSection configSection , IReadOnlyCollection < Assembly > configurationAssemblies , DependencyContext dependencyContext )
46
+ internal ConfigurationReader ( IConfigurationSection configSection , IReadOnlyCollection < Assembly > configurationAssemblies , DependencyContext dependencyContext , SettingValueResolver valueResolver )
47
47
{
48
48
_section = configSection ?? throw new ArgumentNullException ( nameof ( configSection ) ) ;
49
49
_dependencyContext = dependencyContext ;
50
50
_configurationAssemblies = configurationAssemblies ?? throw new ArgumentNullException ( nameof ( configurationAssemblies ) ) ;
51
+ _configuration = valueResolver . AppConfiguration ;
51
52
}
52
53
53
54
public void Configure ( LoggerConfiguration loggerConfiguration )
54
55
{
55
56
var declaredLevelSwitches = ProcessLevelSwitchDeclarations ( ) ;
56
- ApplyMinimumLevel ( loggerConfiguration , declaredLevelSwitches ) ;
57
- ApplyEnrichment ( loggerConfiguration , declaredLevelSwitches ) ;
58
- ApplyFilters ( loggerConfiguration , declaredLevelSwitches ) ;
59
- ApplyDestructuring ( loggerConfiguration , declaredLevelSwitches ) ;
60
- ApplySinks ( loggerConfiguration , declaredLevelSwitches ) ;
61
- ApplyAuditSinks ( loggerConfiguration , declaredLevelSwitches ) ;
57
+ var settingsValueResolver = new SettingValueResolver ( declaredLevelSwitches , _configuration ) ;
58
+
59
+ ApplyMinimumLevel ( loggerConfiguration , settingsValueResolver ) ;
60
+ ApplyEnrichment ( loggerConfiguration , settingsValueResolver ) ;
61
+ ApplyFilters ( loggerConfiguration , settingsValueResolver ) ;
62
+ ApplyDestructuring ( loggerConfiguration , settingsValueResolver ) ;
63
+ ApplySinks ( loggerConfiguration , settingsValueResolver ) ;
64
+ ApplyAuditSinks ( loggerConfiguration , settingsValueResolver ) ;
62
65
}
63
66
64
67
IReadOnlyDictionary < string , LoggingLevelSwitch > ProcessLevelSwitchDeclarations ( )
@@ -89,7 +92,7 @@ IReadOnlyDictionary<string, LoggingLevelSwitch> ProcessLevelSwitchDeclarations()
89
92
return namedSwitches ;
90
93
}
91
94
92
- void ApplyMinimumLevel ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
95
+ void ApplyMinimumLevel ( LoggerConfiguration loggerConfiguration , SettingValueResolver valueResolver )
93
96
{
94
97
var minimumLevelDirective = _section . GetSection ( "MinimumLevel" ) ;
95
98
@@ -102,7 +105,7 @@ void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration, IReadOnlyDiction
102
105
var minLevelControlledByDirective = minimumLevelDirective . GetSection ( "ControlledBy" ) ;
103
106
if ( minLevelControlledByDirective . Value != null )
104
107
{
105
- var globalMinimumLevelSwitch = declaredLevelSwitches . LookUpSwitchByName ( minLevelControlledByDirective . Value ) ;
108
+ var globalMinimumLevelSwitch = valueResolver . LookUpSwitchByName ( minLevelControlledByDirective . Value ) ;
106
109
// not calling ApplyMinimumLevel local function because here we have a reference to a LogLevelSwitch already
107
110
loggerConfiguration . MinimumLevel . ControlledBy ( globalMinimumLevelSwitch ) ;
108
111
}
@@ -117,7 +120,7 @@ void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration, IReadOnlyDiction
117
120
}
118
121
else
119
122
{
120
- var overrideSwitch = declaredLevelSwitches . LookUpSwitchByName ( overridenLevelOrSwitch ) ;
123
+ var overrideSwitch = valueResolver . LookUpSwitchByName ( overridenLevelOrSwitch ) ;
121
124
// not calling ApplyMinimumLevel local function because here we have a reference to a LogLevelSwitch already
122
125
loggerConfiguration . MinimumLevel . Override ( overridePrefix , overrideSwitch ) ;
123
126
}
@@ -142,59 +145,59 @@ void ApplyMinimumLevel(IConfigurationSection directive, Action<LoggerMinimumLeve
142
145
}
143
146
}
144
147
145
- void ApplyFilters ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
148
+ void ApplyFilters ( LoggerConfiguration loggerConfiguration , SettingValueResolver valueResolver )
146
149
{
147
150
var filterDirective = _section . GetSection ( "Filter" ) ;
148
151
if ( filterDirective . GetChildren ( ) . Any ( ) )
149
152
{
150
153
var methodCalls = GetMethodCalls ( filterDirective ) ;
151
- CallConfigurationMethods ( methodCalls , FindFilterConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Filter , declaredLevelSwitches ) ;
154
+ CallConfigurationMethods ( methodCalls , FindFilterConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Filter , valueResolver ) ;
152
155
}
153
156
}
154
157
155
- void ApplyDestructuring ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
158
+ void ApplyDestructuring ( LoggerConfiguration loggerConfiguration , SettingValueResolver valueResolver )
156
159
{
157
160
var destructureDirective = _section . GetSection ( "Destructure" ) ;
158
161
if ( destructureDirective . GetChildren ( ) . Any ( ) )
159
162
{
160
163
var methodCalls = GetMethodCalls ( destructureDirective ) ;
161
- CallConfigurationMethods ( methodCalls , FindDestructureConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Destructure , declaredLevelSwitches ) ;
164
+ CallConfigurationMethods ( methodCalls , FindDestructureConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Destructure , valueResolver ) ;
162
165
}
163
166
}
164
167
165
- void ApplySinks ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
168
+ void ApplySinks ( LoggerConfiguration loggerConfiguration , SettingValueResolver valueResolver )
166
169
{
167
170
var writeToDirective = _section . GetSection ( "WriteTo" ) ;
168
171
if ( writeToDirective . GetChildren ( ) . Any ( ) )
169
172
{
170
173
var methodCalls = GetMethodCalls ( writeToDirective ) ;
171
- CallConfigurationMethods ( methodCalls , FindSinkConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . WriteTo , declaredLevelSwitches ) ;
174
+ CallConfigurationMethods ( methodCalls , FindSinkConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . WriteTo , valueResolver ) ;
172
175
}
173
176
}
174
177
175
- void ApplyAuditSinks ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
178
+ void ApplyAuditSinks ( LoggerConfiguration loggerConfiguration , SettingValueResolver valueResolver )
176
179
{
177
180
var auditToDirective = _section . GetSection ( "AuditTo" ) ;
178
181
if ( auditToDirective . GetChildren ( ) . Any ( ) )
179
182
{
180
183
var methodCalls = GetMethodCalls ( auditToDirective ) ;
181
- CallConfigurationMethods ( methodCalls , FindAuditSinkConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . AuditTo , declaredLevelSwitches ) ;
184
+ CallConfigurationMethods ( methodCalls , FindAuditSinkConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . AuditTo , valueResolver ) ;
182
185
}
183
186
}
184
187
185
- void IConfigurationReader . ApplySinks ( LoggerSinkConfiguration loggerSinkConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
188
+ void IConfigurationReader . ApplySinks ( LoggerSinkConfiguration loggerSinkConfiguration , SettingValueResolver valueResolver )
186
189
{
187
190
var methodCalls = GetMethodCalls ( _section ) ;
188
- CallConfigurationMethods ( methodCalls , FindSinkConfigurationMethods ( _configurationAssemblies ) , loggerSinkConfiguration , declaredLevelSwitches ) ;
191
+ CallConfigurationMethods ( methodCalls , FindSinkConfigurationMethods ( _configurationAssemblies ) , loggerSinkConfiguration , valueResolver ) ;
189
192
}
190
193
191
- void ApplyEnrichment ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
194
+ void ApplyEnrichment ( LoggerConfiguration loggerConfiguration , SettingValueResolver valueResolver )
192
195
{
193
196
var enrichDirective = _section . GetSection ( "Enrich" ) ;
194
197
if ( enrichDirective . GetChildren ( ) . Any ( ) )
195
198
{
196
199
var methodCalls = GetMethodCalls ( enrichDirective ) ;
197
- CallConfigurationMethods ( methodCalls , FindEventEnricherConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Enrich , declaredLevelSwitches ) ;
200
+ CallConfigurationMethods ( methodCalls , FindEventEnricherConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Enrich , valueResolver ) ;
198
201
}
199
202
200
203
var propertiesDirective = _section . GetSection ( "Properties" ) ;
@@ -318,7 +321,7 @@ where filter(assemblyFileName)
318
321
return query . ToArray ( ) ;
319
322
}
320
323
321
- static void CallConfigurationMethods ( ILookup < string , Dictionary < string , IConfigurationArgumentValue > > methods , IList < MethodInfo > configurationMethods , object receiver , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
324
+ static void CallConfigurationMethods ( ILookup < string , Dictionary < string , IConfigurationArgumentValue > > methods , IList < MethodInfo > configurationMethods , object receiver , SettingValueResolver valueResolver )
322
325
{
323
326
foreach ( var method in methods . SelectMany ( g => g . Select ( x => new { g . Key , Value = x } ) ) )
324
327
{
@@ -328,17 +331,19 @@ static void CallConfigurationMethods(ILookup<string, Dictionary<string, IConfigu
328
331
{
329
332
var call = ( from p in methodInfo . GetParameters ( ) . Skip ( 1 )
330
333
let directive = method . Value . FirstOrDefault ( s => s . Key . Equals ( p . Name , StringComparison . OrdinalIgnoreCase ) )
331
- select directive . Key == null ? p . DefaultValue : directive . Value . ConvertTo ( p . ParameterType , declaredLevelSwitches ) ) . ToList ( ) ;
334
+ select directive . Key == null
335
+ ? p . DefaultValue
336
+ : directive . Value . ConvertTo ( p . ParameterType , valueResolver ) ) . ToList ( ) ;
332
337
333
338
var parm = methodInfo . GetParameters ( ) . FirstOrDefault ( i => i . ParameterType == typeof ( IConfiguration ) ) ;
334
339
if ( parm != null && ! parm . HasDefaultValue )
335
340
{
336
- if ( _configuration is null )
341
+ if ( valueResolver . AppConfiguration is null )
337
342
{
338
343
throw new InvalidOperationException ( "Trying to invoke a configuration method accepting a `IConfiguration` argument. " +
339
344
$ "This is not supported when only a `IConfigSection` has been provided. (method '{ methodInfo } ')") ;
340
345
}
341
- call [ parm . Position - 1 ] = _configuration ;
346
+ call [ parm . Position - 1 ] = valueResolver . AppConfiguration ;
342
347
}
343
348
344
349
call . Insert ( 0 , receiver ) ;
0 commit comments