Skip to content

Commit 90b87ba

Browse files
committed
changed null-ref checks to Any() tests on GetSection results
1 parent 7e14841 commit 90b87ba

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,27 @@ IReadOnlyDictionary<string, LoggingLevelSwitch> ProcessLevelSwitchDeclarations()
5959
{
6060
var levelSwitchesDirective = _configuration.GetSection("LevelSwitches");
6161
var namedSwitches = new Dictionary<string, LoggingLevelSwitch>();
62-
if (levelSwitchesDirective != null)
62+
foreach (var levelSwitchDeclaration in levelSwitchesDirective.GetChildren())
6363
{
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))
6568
{
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();
8475
}
76+
else
77+
{
78+
var initialLevel = ParseLogEventLevel(switchInitialLevel);
79+
newSwitch = new LoggingLevelSwitch(initialLevel);
80+
}
81+
namedSwitches.Add(switchName, newSwitch);
8582
}
86-
8783
return namedSwitches;
8884
}
8985

@@ -98,7 +94,7 @@ void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration, IReadOnlyDiction
9894
}
9995

10096
var minLevelControlledByDirective = minimumLevelDirective.GetSection("ControlledBy");
101-
if (minLevelControlledByDirective?.Value != null)
97+
if (minLevelControlledByDirective.Value != null)
10298
{
10399
var globalMinimumLevelSwitch = declaredLevelSwitches.LookUpSwitchByName(minLevelControlledByDirective.Value);
104100
// 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
143139
void ApplyFilters(LoggerConfiguration loggerConfiguration, IReadOnlyDictionary<string, LoggingLevelSwitch> declaredLevelSwitches)
144140
{
145141
var filterDirective = _configuration.GetSection("Filter");
146-
if (filterDirective != null)
142+
if (filterDirective.GetChildren().Any())
147143
{
148144
var methodCalls = GetMethodCalls(filterDirective);
149145
CallConfigurationMethods(methodCalls, FindFilterConfigurationMethods(_configurationAssemblies), loggerConfiguration.Filter, declaredLevelSwitches);
@@ -153,7 +149,7 @@ void ApplyFilters(LoggerConfiguration loggerConfiguration, IReadOnlyDictionary<s
153149
void ApplySinks(LoggerConfiguration loggerConfiguration, IReadOnlyDictionary<string, LoggingLevelSwitch> declaredLevelSwitches)
154150
{
155151
var writeToDirective = _configuration.GetSection("WriteTo");
156-
if (writeToDirective != null)
152+
if (writeToDirective.GetChildren().Any())
157153
{
158154
var methodCalls = GetMethodCalls(writeToDirective);
159155
CallConfigurationMethods(methodCalls, FindSinkConfigurationMethods(_configurationAssemblies), loggerConfiguration.WriteTo, declaredLevelSwitches);
@@ -163,7 +159,7 @@ void ApplySinks(LoggerConfiguration loggerConfiguration, IReadOnlyDictionary<str
163159
void ApplyAuditSinks(LoggerConfiguration loggerConfiguration, IReadOnlyDictionary<string, LoggingLevelSwitch> declaredLevelSwitches)
164160
{
165161
var auditToDirective = _configuration.GetSection("AuditTo");
166-
if (auditToDirective != null)
162+
if (auditToDirective.GetChildren().Any())
167163
{
168164
var methodCalls = GetMethodCalls(auditToDirective);
169165
CallConfigurationMethods(methodCalls, FindAuditSinkConfigurationMethods(_configurationAssemblies), loggerConfiguration.AuditTo, declaredLevelSwitches);
@@ -179,14 +175,14 @@ void IConfigurationReader.ApplySinks(LoggerSinkConfiguration loggerSinkConfigura
179175
void ApplyEnrichment(LoggerConfiguration loggerConfiguration, IReadOnlyDictionary<string, LoggingLevelSwitch> declaredLevelSwitches)
180176
{
181177
var enrichDirective = _configuration.GetSection("Enrich");
182-
if (enrichDirective != null)
178+
if (enrichDirective.GetChildren().Any())
183179
{
184180
var methodCalls = GetMethodCalls(enrichDirective);
185181
CallConfigurationMethods(methodCalls, FindEventEnricherConfigurationMethods(_configurationAssemblies), loggerConfiguration.Enrich, declaredLevelSwitches);
186182
}
187183

188184
var propertiesDirective = _configuration.GetSection("Properties");
189-
if (propertiesDirective != null)
185+
if (propertiesDirective.GetChildren().Any())
190186
{
191187
foreach (var enrichProperyDirective in propertiesDirective.GetChildren())
192188
{
@@ -250,7 +246,7 @@ IReadOnlyCollection<Assembly> LoadConfigurationAssemblies()
250246
var assemblies = new Dictionary<string, Assembly>();
251247

252248
var usingSection = _configuration.GetSection("Using");
253-
if (usingSection != null)
249+
if (usingSection.GetChildren().Any())
254250
{
255251
foreach (var simpleName in usingSection.GetChildren().Select(c => c.Value))
256252
{

0 commit comments

Comments
 (0)