1717using Microsoft . Extensions . DependencyModel ;
1818using Serilog . Configuration ;
1919using Serilog . Settings . Configuration ;
20- using System . Reflection ;
20+ using Serilog . Settings . Configuration . Assemblies ;
2121
2222namespace Serilog
2323{
@@ -32,28 +32,53 @@ public static class ConfigurationLoggerConfigurationExtensions
3232 public const string DefaultSectionName = "Serilog" ;
3333
3434 /// <summary>
35- /// Reads logger settings from the provided configuration object using the default section name. Generally this
35+ /// Reads logger settings from the provided configuration object using the provided section name. Generally this
3636 /// is preferable over the other method that takes a configuration section. Only this version will populate
3737 /// IConfiguration parameters on target methods.
3838 /// </summary>
3939 /// <param name="settingConfiguration">Logger setting configuration.</param>
4040 /// <param name="configuration">A configuration object which contains a Serilog section.</param>
41+ /// <param name="sectionName">A section name for section which contains a Serilog section.</param>
4142 /// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform
4243 /// default will be used.</param>
4344 /// <returns>An object allowing configuration to continue.</returns>
4445 public static LoggerConfiguration Configuration (
4546 this LoggerSettingsConfiguration settingConfiguration ,
4647 IConfiguration configuration ,
48+ string sectionName ,
4749 DependencyContext dependencyContext = null )
4850 {
51+ if ( settingConfiguration == null ) throw new ArgumentNullException ( nameof ( settingConfiguration ) ) ;
4952 if ( configuration == null ) throw new ArgumentNullException ( nameof ( configuration ) ) ;
53+ if ( sectionName == null ) throw new ArgumentNullException ( nameof ( sectionName ) ) ;
54+
55+ var assemblyFinder = dependencyContext == null
56+ ? AssemblyFinder . Auto ( )
57+ : AssemblyFinder . ForDependencyContext ( dependencyContext ) ;
5058
5159 return settingConfiguration . Settings (
5260 new ConfigurationReader (
53- configuration ,
54- dependencyContext ?? ( Assembly . GetEntryAssembly ( ) != null ? DependencyContext . Default : null ) ) ) ;
61+ configuration . GetSection ( sectionName ) ,
62+ assemblyFinder ,
63+ configuration ) ) ;
5564 }
5665
66+ /// <summary>
67+ /// Reads logger settings from the provided configuration object using the default section name. Generally this
68+ /// is preferable over the other method that takes a configuration section. Only this version will populate
69+ /// IConfiguration parameters on target methods.
70+ /// </summary>
71+ /// <param name="settingConfiguration">Logger setting configuration.</param>
72+ /// <param name="configuration">A configuration object which contains a Serilog section.</param>
73+ /// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform
74+ /// default will be used.</param>
75+ /// <returns>An object allowing configuration to continue.</returns>
76+ public static LoggerConfiguration Configuration (
77+ this LoggerSettingsConfiguration settingConfiguration ,
78+ IConfiguration configuration ,
79+ DependencyContext dependencyContext = null )
80+ => Configuration ( settingConfiguration , configuration , DefaultSectionName , dependencyContext ) ;
81+
5782 /// <summary>
5883 /// Reads logger settings from the provided configuration section. Generally it is preferable to use the other
5984 /// extension method that takes the full configuration object.
@@ -63,6 +88,7 @@ public static LoggerConfiguration Configuration(
6388 /// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform
6489 /// default will be used.</param>
6590 /// <returns>An object allowing configuration to continue.</returns>
91+ [ Obsolete ( "Use ReadFrom.Configuration(IConfiguration configuration, string sectionName, DependencyContext dependencyContext) instead." ) ]
6692 public static LoggerConfiguration ConfigurationSection (
6793 this LoggerSettingsConfiguration settingConfiguration ,
6894 IConfigurationSection configSection ,
@@ -71,38 +97,57 @@ public static LoggerConfiguration ConfigurationSection(
7197 if ( settingConfiguration == null ) throw new ArgumentNullException ( nameof ( settingConfiguration ) ) ;
7298 if ( configSection == null ) throw new ArgumentNullException ( nameof ( configSection ) ) ;
7399
100+ var assemblyFinder = dependencyContext == null
101+ ? AssemblyFinder . Auto ( )
102+ : AssemblyFinder . ForDependencyContext ( dependencyContext ) ;
103+
74104 return settingConfiguration . Settings (
75105 new ConfigurationReader (
76106 configSection ,
77- dependencyContext ?? ( Assembly . GetEntryAssembly ( ) != null ? DependencyContext . Default : null ) ) ) ;
107+ assemblyFinder ,
108+ configuration : null ) ) ;
78109 }
79110
80111 /// <summary>
81- /// Reads logger settings from the provided configuration object using the default section name. Generally this
112+ /// Reads logger settings from the provided configuration object using the provided section name. Generally this
82113 /// is preferable over the other method that takes a configuration section. Only this version will populate
83114 /// IConfiguration parameters on target methods.
84115 /// </summary>
85116 /// <param name="settingConfiguration">Logger setting configuration.</param>
86117 /// <param name="configuration">A configuration object which contains a Serilog section.</param>
118+ /// <param name="sectionName">A section name for section which contains a Serilog section.</param>
87119 /// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
88120 /// <returns>An object allowing configuration to continue.</returns>
89121 public static LoggerConfiguration Configuration (
90122 this LoggerSettingsConfiguration settingConfiguration ,
91123 IConfiguration configuration ,
124+ string sectionName ,
92125 ConfigurationAssemblySource configurationAssemblySource )
93126 {
127+ if ( settingConfiguration == null ) throw new ArgumentNullException ( nameof ( settingConfiguration ) ) ;
94128 if ( configuration == null ) throw new ArgumentNullException ( nameof ( configuration ) ) ;
129+ if ( sectionName == null ) throw new ArgumentNullException ( nameof ( sectionName ) ) ;
130+
131+ var assemblyFinder = AssemblyFinder . ForSource ( configurationAssemblySource ) ;
95132
96- if ( configurationAssemblySource == ConfigurationAssemblySource . UseLoadedAssemblies )
97- {
98- return Configuration ( settingConfiguration , configuration ) ;
99- }
100- else
101- {
102- return settingConfiguration . Settings ( new ConfigurationReader ( configuration , null ) ) ;
103- }
133+ return settingConfiguration . Settings ( new ConfigurationReader ( configuration . GetSection ( sectionName ) , assemblyFinder , configuration ) ) ;
104134 }
105135
136+ /// <summary>
137+ /// Reads logger settings from the provided configuration object using the default section name. Generally this
138+ /// is preferable over the other method that takes a configuration section. Only this version will populate
139+ /// IConfiguration parameters on target methods.
140+ /// </summary>
141+ /// <param name="settingConfiguration">Logger setting configuration.</param>
142+ /// <param name="configuration">A configuration object which contains a Serilog section.</param>
143+ /// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
144+ /// <returns>An object allowing configuration to continue.</returns>
145+ public static LoggerConfiguration Configuration (
146+ this LoggerSettingsConfiguration settingConfiguration ,
147+ IConfiguration configuration ,
148+ ConfigurationAssemblySource configurationAssemblySource )
149+ => Configuration ( settingConfiguration , configuration , DefaultSectionName , configurationAssemblySource ) ;
150+
106151 /// <summary>
107152 /// Reads logger settings from the provided configuration section. Generally it is preferable to use the other
108153 /// extension method that takes the full configuration object.
@@ -111,6 +156,7 @@ public static LoggerConfiguration Configuration(
111156 /// <param name="configSection">The Serilog configuration section</param>
112157 /// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
113158 /// <returns>An object allowing configuration to continue.</returns>
159+ [ Obsolete ( "Use ReadFrom.Configuration(IConfiguration configuration, string sectionName, ConfigurationAssemblySource configurationAssemblySource) instead." ) ]
114160 public static LoggerConfiguration ConfigurationSection (
115161 this LoggerSettingsConfiguration settingConfiguration ,
116162 IConfigurationSection configSection ,
@@ -119,14 +165,9 @@ public static LoggerConfiguration ConfigurationSection(
119165 if ( settingConfiguration == null ) throw new ArgumentNullException ( nameof ( settingConfiguration ) ) ;
120166 if ( configSection == null ) throw new ArgumentNullException ( nameof ( configSection ) ) ;
121167
122- if ( configurationAssemblySource == ConfigurationAssemblySource . UseLoadedAssemblies )
123- {
124- return Configuration ( settingConfiguration , configSection ) ;
125- }
126- else
127- {
128- return settingConfiguration . Settings ( new ConfigurationReader ( configSection , null ) ) ;
129- }
168+ var assemblyFinder = AssemblyFinder . ForSource ( configurationAssemblySource ) ;
169+
170+ return settingConfiguration . Settings ( new ConfigurationReader ( configSection , assemblyFinder , configuration : null ) ) ;
130171 }
131172 }
132173}
0 commit comments