17
17
using Microsoft . Extensions . DependencyModel ;
18
18
using Serilog . Configuration ;
19
19
using Serilog . Settings . Configuration ;
20
- using System . Reflection ;
20
+ using Serilog . Settings . Configuration . Assemblies ;
21
21
22
22
namespace Serilog
23
23
{
@@ -32,28 +32,53 @@ public static class ConfigurationLoggerConfigurationExtensions
32
32
public const string DefaultSectionName = "Serilog" ;
33
33
34
34
/// <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
36
36
/// is preferable over the other method that takes a configuration section. Only this version will populate
37
37
/// IConfiguration parameters on target methods.
38
38
/// </summary>
39
39
/// <param name="settingConfiguration">Logger setting configuration.</param>
40
40
/// <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>
41
42
/// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform
42
43
/// default will be used.</param>
43
44
/// <returns>An object allowing configuration to continue.</returns>
44
45
public static LoggerConfiguration Configuration (
45
46
this LoggerSettingsConfiguration settingConfiguration ,
46
47
IConfiguration configuration ,
48
+ string sectionName ,
47
49
DependencyContext dependencyContext = null )
48
50
{
51
+ if ( settingConfiguration == null ) throw new ArgumentNullException ( nameof ( settingConfiguration ) ) ;
49
52
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 ) ;
50
58
51
59
return settingConfiguration . Settings (
52
60
new ConfigurationReader (
53
- configuration ,
54
- dependencyContext ?? ( Assembly . GetEntryAssembly ( ) != null ? DependencyContext . Default : null ) ) ) ;
61
+ configuration . GetSection ( sectionName ) ,
62
+ assemblyFinder ,
63
+ configuration ) ) ;
55
64
}
56
65
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
+
57
82
/// <summary>
58
83
/// Reads logger settings from the provided configuration section. Generally it is preferable to use the other
59
84
/// extension method that takes the full configuration object.
@@ -63,6 +88,7 @@ public static LoggerConfiguration Configuration(
63
88
/// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform
64
89
/// default will be used.</param>
65
90
/// <returns>An object allowing configuration to continue.</returns>
91
+ [ Obsolete ( "Use ReadFrom.Configuration(IConfiguration configuration, string sectionName, DependencyContext dependencyContext) instead." ) ]
66
92
public static LoggerConfiguration ConfigurationSection (
67
93
this LoggerSettingsConfiguration settingConfiguration ,
68
94
IConfigurationSection configSection ,
@@ -71,38 +97,57 @@ public static LoggerConfiguration ConfigurationSection(
71
97
if ( settingConfiguration == null ) throw new ArgumentNullException ( nameof ( settingConfiguration ) ) ;
72
98
if ( configSection == null ) throw new ArgumentNullException ( nameof ( configSection ) ) ;
73
99
100
+ var assemblyFinder = dependencyContext == null
101
+ ? AssemblyFinder . Auto ( )
102
+ : AssemblyFinder . ForDependencyContext ( dependencyContext ) ;
103
+
74
104
return settingConfiguration . Settings (
75
105
new ConfigurationReader (
76
106
configSection ,
77
- dependencyContext ?? ( Assembly . GetEntryAssembly ( ) != null ? DependencyContext . Default : null ) ) ) ;
107
+ assemblyFinder ,
108
+ configuration : null ) ) ;
78
109
}
79
110
80
111
/// <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
82
113
/// is preferable over the other method that takes a configuration section. Only this version will populate
83
114
/// IConfiguration parameters on target methods.
84
115
/// </summary>
85
116
/// <param name="settingConfiguration">Logger setting configuration.</param>
86
117
/// <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>
87
119
/// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
88
120
/// <returns>An object allowing configuration to continue.</returns>
89
121
public static LoggerConfiguration Configuration (
90
122
this LoggerSettingsConfiguration settingConfiguration ,
91
123
IConfiguration configuration ,
124
+ string sectionName ,
92
125
ConfigurationAssemblySource configurationAssemblySource )
93
126
{
127
+ if ( settingConfiguration == null ) throw new ArgumentNullException ( nameof ( settingConfiguration ) ) ;
94
128
if ( configuration == null ) throw new ArgumentNullException ( nameof ( configuration ) ) ;
129
+ if ( sectionName == null ) throw new ArgumentNullException ( nameof ( sectionName ) ) ;
130
+
131
+ var assemblyFinder = AssemblyFinder . ForSource ( configurationAssemblySource ) ;
95
132
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 ) ) ;
104
134
}
105
135
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
+
106
151
/// <summary>
107
152
/// Reads logger settings from the provided configuration section. Generally it is preferable to use the other
108
153
/// extension method that takes the full configuration object.
@@ -111,6 +156,7 @@ public static LoggerConfiguration Configuration(
111
156
/// <param name="configSection">The Serilog configuration section</param>
112
157
/// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
113
158
/// <returns>An object allowing configuration to continue.</returns>
159
+ [ Obsolete ( "Use ReadFrom.Configuration(IConfiguration configuration, string sectionName, ConfigurationAssemblySource configurationAssemblySource) instead." ) ]
114
160
public static LoggerConfiguration ConfigurationSection (
115
161
this LoggerSettingsConfiguration settingConfiguration ,
116
162
IConfigurationSection configSection ,
@@ -119,14 +165,9 @@ public static LoggerConfiguration ConfigurationSection(
119
165
if ( settingConfiguration == null ) throw new ArgumentNullException ( nameof ( settingConfiguration ) ) ;
120
166
if ( configSection == null ) throw new ArgumentNullException ( nameof ( configSection ) ) ;
121
167
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 ) ) ;
130
171
}
131
172
}
132
173
}
0 commit comments