@@ -59,31 +59,27 @@ IReadOnlyDictionary<string, LoggingLevelSwitch> ProcessLevelSwitchDeclarations()
59
59
{
60
60
var levelSwitchesDirective = _configuration . GetSection ( "LevelSwitches" ) ;
61
61
var namedSwitches = new Dictionary < string , LoggingLevelSwitch > ( ) ;
62
- if ( levelSwitchesDirective != null )
62
+ foreach ( var levelSwitchDeclaration in levelSwitchesDirective . GetChildren ( ) )
63
63
{
64
- foreach ( var levelSwitchDeclaration in levelSwitchesDirective . GetChildren ( ) )
64
+ var switchName = levelSwitchDeclaration . Key ;
65
+ var switchInitialLevel = levelSwitchDeclaration . Value ;
66
+ // switchName must be something like $switch to avoid ambiguities
67
+ if ( ! IsValidSwitchName ( switchName ) )
65
68
{
66
- var switchName = levelSwitchDeclaration . Key ;
67
- var switchInitialLevel = levelSwitchDeclaration . Value ;
68
- // switchName must be something like $switch to avoid ambiguities
69
- if ( ! IsValidSwitchName ( switchName ) )
70
- {
71
- throw new FormatException ( $ "\" { switchName } \" is not a valid name for a Level Switch declaration. Level switch must be declared with a '$' sign, like \" LevelSwitches\" : {{\" $switchName\" : \" InitialLevel\" }}") ;
72
- }
73
- LoggingLevelSwitch newSwitch ;
74
- if ( string . IsNullOrEmpty ( switchInitialLevel ) )
75
- {
76
- newSwitch = new LoggingLevelSwitch ( ) ;
77
- }
78
- else
79
- {
80
- var initialLevel = ParseLogEventLevel ( switchInitialLevel ) ;
81
- newSwitch = new LoggingLevelSwitch ( initialLevel ) ;
82
- }
83
- namedSwitches . Add ( switchName , newSwitch ) ;
69
+ throw new FormatException ( $ "\" { switchName } \" is not a valid name for a Level Switch declaration. Level switch must be declared with a '$' sign, like \" LevelSwitches\" : {{\" $switchName\" : \" InitialLevel\" }}") ;
70
+ }
71
+ LoggingLevelSwitch newSwitch ;
72
+ if ( string . IsNullOrEmpty ( switchInitialLevel ) )
73
+ {
74
+ newSwitch = new LoggingLevelSwitch ( ) ;
84
75
}
76
+ else
77
+ {
78
+ var initialLevel = ParseLogEventLevel ( switchInitialLevel ) ;
79
+ newSwitch = new LoggingLevelSwitch ( initialLevel ) ;
80
+ }
81
+ namedSwitches . Add ( switchName , newSwitch ) ;
85
82
}
86
-
87
83
return namedSwitches ;
88
84
}
89
85
@@ -98,7 +94,7 @@ void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration, IReadOnlyDiction
98
94
}
99
95
100
96
var minLevelControlledByDirective = minimumLevelDirective . GetSection ( "ControlledBy" ) ;
101
- if ( minLevelControlledByDirective ? . Value != null )
97
+ if ( minLevelControlledByDirective . Value != null )
102
98
{
103
99
var globalMinimumLevelSwitch = declaredLevelSwitches . LookUpSwitchByName ( minLevelControlledByDirective . Value ) ;
104
100
// not calling ApplyMinimumLevel local function because here we have a reference to a LogLevelSwitch already
@@ -143,7 +139,7 @@ void ApplyMinimumLevel(IConfigurationSection directive, Action<LoggerMinimumLeve
143
139
void ApplyFilters ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
144
140
{
145
141
var filterDirective = _configuration . GetSection ( "Filter" ) ;
146
- if ( filterDirective != null )
142
+ if ( filterDirective . GetChildren ( ) . Any ( ) )
147
143
{
148
144
var methodCalls = GetMethodCalls ( filterDirective ) ;
149
145
CallConfigurationMethods ( methodCalls , FindFilterConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Filter , declaredLevelSwitches ) ;
@@ -153,7 +149,7 @@ void ApplyFilters(LoggerConfiguration loggerConfiguration, IReadOnlyDictionary<s
153
149
void ApplySinks ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
154
150
{
155
151
var writeToDirective = _configuration . GetSection ( "WriteTo" ) ;
156
- if ( writeToDirective != null )
152
+ if ( writeToDirective . GetChildren ( ) . Any ( ) )
157
153
{
158
154
var methodCalls = GetMethodCalls ( writeToDirective ) ;
159
155
CallConfigurationMethods ( methodCalls , FindSinkConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . WriteTo , declaredLevelSwitches ) ;
@@ -163,7 +159,7 @@ void ApplySinks(LoggerConfiguration loggerConfiguration, IReadOnlyDictionary<str
163
159
void ApplyAuditSinks ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
164
160
{
165
161
var auditToDirective = _configuration . GetSection ( "AuditTo" ) ;
166
- if ( auditToDirective != null )
162
+ if ( auditToDirective . GetChildren ( ) . Any ( ) )
167
163
{
168
164
var methodCalls = GetMethodCalls ( auditToDirective ) ;
169
165
CallConfigurationMethods ( methodCalls , FindAuditSinkConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . AuditTo , declaredLevelSwitches ) ;
@@ -179,14 +175,14 @@ void IConfigurationReader.ApplySinks(LoggerSinkConfiguration loggerSinkConfigura
179
175
void ApplyEnrichment ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
180
176
{
181
177
var enrichDirective = _configuration . GetSection ( "Enrich" ) ;
182
- if ( enrichDirective != null )
178
+ if ( enrichDirective . GetChildren ( ) . Any ( ) )
183
179
{
184
180
var methodCalls = GetMethodCalls ( enrichDirective ) ;
185
181
CallConfigurationMethods ( methodCalls , FindEventEnricherConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Enrich , declaredLevelSwitches ) ;
186
182
}
187
183
188
184
var propertiesDirective = _configuration . GetSection ( "Properties" ) ;
189
- if ( propertiesDirective != null )
185
+ if ( propertiesDirective . GetChildren ( ) . Any ( ) )
190
186
{
191
187
foreach ( var enrichProperyDirective in propertiesDirective . GetChildren ( ) )
192
188
{
@@ -250,7 +246,7 @@ IReadOnlyCollection<Assembly> LoadConfigurationAssemblies()
250
246
var assemblies = new Dictionary < string , Assembly > ( ) ;
251
247
252
248
var usingSection = _configuration . GetSection ( "Using" ) ;
253
- if ( usingSection != null )
249
+ if ( usingSection . GetChildren ( ) . Any ( ) )
254
250
{
255
251
foreach ( var simpleName in usingSection . GetChildren ( ) . Select ( c => c . Value ) )
256
252
{
0 commit comments