Skip to content

Commit 1f24471

Browse files
committed
PR feedback
1 parent 0d4b69e commit 1f24471

File tree

3 files changed

+95
-35
lines changed

3 files changed

+95
-35
lines changed

src/Serilog.Settings.Configuration/ConfigurationLoggerConfigurationExtensions.cs

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,6 @@
2121

2222
namespace Serilog
2323
{
24-
/// <summary>
25-
/// Defines the interpretation of a null DependencyContext configuration parameter.
26-
/// </summary>
27-
public enum ConfigurationAssemblySource
28-
{
29-
/// <summary>
30-
/// Try to scan the assemblies already in memory. This is the default. If GetEntryAssembly is null, fallback to DLL scanning.
31-
/// </summary>
32-
UseLoadedAssemblies,
33-
34-
/// <summary>
35-
/// Scan for assemblies in DLLs from the working directory. This is the fallback when GetEntryAssembly is null.
36-
/// </summary>
37-
AlwaysScanDllFiles
38-
}
39-
4024
/// <summary>
4125
/// Extends <see cref="LoggerConfiguration"/> with support for System.Configuration appSettings elements.
4226
/// </summary>
@@ -54,23 +38,20 @@ public static class ConfigurationLoggerConfigurationExtensions
5438
/// </summary>
5539
/// <param name="settingConfiguration">Logger setting configuration.</param>
5640
/// <param name="configuration">A configuration object which contains a Serilog section.</param>
57-
/// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located.</param>
58-
/// <param name="onNullDependencyContext">If dependency context is not supplied, either the platform default or DLL scanning may be used.</param>
41+
/// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform
42+
/// default will be used.</param>
5943
/// <returns>An object allowing configuration to continue.</returns>
6044
public static LoggerConfiguration Configuration(
6145
this LoggerSettingsConfiguration settingConfiguration,
6246
IConfiguration configuration,
63-
DependencyContext dependencyContext = null,
64-
ConfigurationAssemblySource onNullDependencyContext = ConfigurationAssemblySource.UseLoadedAssemblies)
47+
DependencyContext dependencyContext = null)
6548
{
6649
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
50+
6751
return settingConfiguration.Settings(
6852
new ConfigurationReader(
6953
configuration,
70-
dependencyContext ??
71-
(onNullDependencyContext == ConfigurationAssemblySource.UseLoadedAssemblies
72-
&& Assembly.GetEntryAssembly() != null
73-
? DependencyContext.Default : null)));
54+
dependencyContext ?? (Assembly.GetEntryAssembly() != null ? DependencyContext.Default : null)));
7455
}
7556

7657
/// <summary>
@@ -79,25 +60,73 @@ public static LoggerConfiguration Configuration(
7960
/// </summary>
8061
/// <param name="settingConfiguration">Logger setting configuration.</param>
8162
/// <param name="configSection">The Serilog configuration section</param>
82-
/// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located.</param>
83-
/// <param name="onNullDependencyContext">If dependency context is not supplied, either the platform default or DLL scanning may be used.</param>
63+
/// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform
64+
/// default will be used.</param>
8465
/// <returns>An object allowing configuration to continue.</returns>
8566
public static LoggerConfiguration ConfigurationSection(
8667
this LoggerSettingsConfiguration settingConfiguration,
8768
IConfigurationSection configSection,
88-
DependencyContext dependencyContext = null,
89-
ConfigurationAssemblySource onNullDependencyContext = ConfigurationAssemblySource.UseLoadedAssemblies)
69+
DependencyContext dependencyContext = null)
9070
{
9171
if (settingConfiguration == null) throw new ArgumentNullException(nameof(settingConfiguration));
9272
if (configSection == null) throw new ArgumentNullException(nameof(configSection));
93-
73+
9474
return settingConfiguration.Settings(
9575
new ConfigurationReader(
9676
configSection,
97-
dependencyContext ??
98-
(onNullDependencyContext == ConfigurationAssemblySource.UseLoadedAssemblies
99-
&& Assembly.GetEntryAssembly() != null
100-
? DependencyContext.Default : null)));
77+
dependencyContext ?? (Assembly.GetEntryAssembly() != null ? DependencyContext.Default : null)));
78+
}
79+
80+
/// <summary>
81+
/// Reads logger settings from the provided configuration object using the default section name. Generally this
82+
/// is preferable over the other method that takes a configuration section. Only this version will populate
83+
/// IConfiguration parameters on target methods.
84+
/// </summary>
85+
/// <param name="settingConfiguration">Logger setting configuration.</param>
86+
/// <param name="configuration">A configuration object which contains a Serilog section.</param>
87+
/// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
88+
/// <returns>An object allowing configuration to continue.</returns>
89+
public static LoggerConfiguration Configuration(
90+
this LoggerSettingsConfiguration settingConfiguration,
91+
IConfiguration configuration,
92+
ConfigurationAssemblySource configurationAssemblySource)
93+
{
94+
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
95+
96+
if(configurationAssemblySource == ConfigurationAssemblySource.UseLoadedAssemblies)
97+
{
98+
return Configuration(settingConfiguration, configuration);
99+
}
100+
else
101+
{
102+
return settingConfiguration.Settings(new ConfigurationReader(configuration, null));
103+
}
104+
}
105+
106+
/// <summary>
107+
/// Reads logger settings from the provided configuration section. Generally it is preferable to use the other
108+
/// extension method that takes the full configuration object.
109+
/// </summary>
110+
/// <param name="settingConfiguration">Logger setting configuration.</param>
111+
/// <param name="configSection">The Serilog configuration section</param>
112+
/// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
113+
/// <returns>An object allowing configuration to continue.</returns>
114+
public static LoggerConfiguration ConfigurationSection(
115+
this LoggerSettingsConfiguration settingConfiguration,
116+
IConfigurationSection configSection,
117+
ConfigurationAssemblySource configurationAssemblySource)
118+
{
119+
if (settingConfiguration == null) throw new ArgumentNullException(nameof(settingConfiguration));
120+
if (configSection == null) throw new ArgumentNullException(nameof(configSection));
121+
122+
if (configurationAssemblySource == ConfigurationAssemblySource.UseLoadedAssemblies)
123+
{
124+
return Configuration(settingConfiguration, configSection);
125+
}
126+
else
127+
{
128+
return settingConfiguration.Settings(new ConfigurationReader(configSection, null));
129+
}
101130
}
102131
}
103132
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2013-2016 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Serilog.Settings.Configuration
16+
{
17+
/// <summary>
18+
/// Defines how the package will identify the assemblies which are scanned for sinks and other Type information.
19+
/// </summary>
20+
public enum ConfigurationAssemblySource
21+
{
22+
/// <summary>
23+
/// Try to scan the assemblies already in memory. This is the default. If GetEntryAssembly is null, fallback to DLL scanning.
24+
/// </summary>
25+
UseLoadedAssemblies,
26+
27+
/// <summary>
28+
/// Scan for assemblies in DLLs from the working directory. This is the fallback when GetEntryAssembly is null.
29+
/// </summary>
30+
AlwaysScanDllFiles
31+
}
32+
}

test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ public void ConfigurationAssembliesFromDllScanning()
9292
var log = new LoggerConfiguration()
9393
.ReadFrom.Configuration(
9494
configuration: config,
95-
dependencyContext: null,
96-
onNullDependencyContext: ConfigurationAssemblySource.AlwaysScanDllFiles)
95+
configurationAssemblySource: ConfigurationAssemblySource.AlwaysScanDllFiles)
9796
.CreateLogger();
9897

9998
DummyConsoleSink.Emitted.Clear();

0 commit comments

Comments
 (0)