2121
2222namespace 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}
0 commit comments