Skip to content

Commit 5dec5fd

Browse files
author
Sergey Komisarchik
committed
added new ReadFrom.Configuration(...) overloads; marked old as obsolete
1 parent 3cf169d commit 5dec5fd

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

src/Serilog.Settings.Configuration/ConfigurationLoggerConfigurationExtensions.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,52 @@ 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
{
4951
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
52+
if (sectionName == null) throw new ArgumentNullException(nameof(sectionName));
5053

5154
var assemblyFinder = dependencyContext == null
5255
? AssemblyFinder.Auto()
5356
: AssemblyFinder.ForDependencyContext(dependencyContext);
5457

5558
return settingConfiguration.Settings(
5659
new ConfigurationReader(
57-
configuration.GetSection(DefaultSectionName),
60+
configuration.GetSection(sectionName),
5861
assemblyFinder,
5962
configuration));
6063
}
6164

65+
/// <summary>
66+
/// Reads logger settings from the provided configuration object using the default section name. Generally this
67+
/// is preferable over the other method that takes a configuration section. Only this version will populate
68+
/// IConfiguration parameters on target methods.
69+
/// </summary>
70+
/// <param name="settingConfiguration">Logger setting configuration.</param>
71+
/// <param name="configuration">A configuration object which contains a Serilog section.</param>
72+
/// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform
73+
/// default will be used.</param>
74+
/// <returns>An object allowing configuration to continue.</returns>
75+
public static LoggerConfiguration Configuration(
76+
this LoggerSettingsConfiguration settingConfiguration,
77+
IConfiguration configuration,
78+
DependencyContext dependencyContext = null)
79+
=> Configuration(settingConfiguration, configuration, DefaultSectionName, dependencyContext);
80+
6281
/// <summary>
6382
/// Reads logger settings from the provided configuration section. Generally it is preferable to use the other
6483
/// extension method that takes the full configuration object.
@@ -68,6 +87,7 @@ public static LoggerConfiguration Configuration(
6887
/// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform
6988
/// default will be used.</param>
7089
/// <returns>An object allowing configuration to continue.</returns>
90+
[Obsolete("Use ReadFrom.Configuration(IConfiguration configuration, string sectionName, DependencyContext dependencyContext) instead.")]
7191
public static LoggerConfiguration ConfigurationSection(
7292
this LoggerSettingsConfiguration settingConfiguration,
7393
IConfigurationSection configSection,
@@ -88,26 +108,44 @@ public static LoggerConfiguration ConfigurationSection(
88108
}
89109

90110
/// <summary>
91-
/// Reads logger settings from the provided configuration object using the default section name. Generally this
111+
/// Reads logger settings from the provided configuration object using the provided section name. Generally this
92112
/// is preferable over the other method that takes a configuration section. Only this version will populate
93113
/// IConfiguration parameters on target methods.
94114
/// </summary>
95115
/// <param name="settingConfiguration">Logger setting configuration.</param>
96116
/// <param name="configuration">A configuration object which contains a Serilog section.</param>
117+
/// <param name="sectionName">A section name for section which contains a Serilog section.</param>
97118
/// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
98119
/// <returns>An object allowing configuration to continue.</returns>
99120
public static LoggerConfiguration Configuration(
100121
this LoggerSettingsConfiguration settingConfiguration,
101122
IConfiguration configuration,
123+
string sectionName,
102124
ConfigurationAssemblySource configurationAssemblySource)
103125
{
104126
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
127+
if (sectionName == null) throw new ArgumentNullException(nameof(sectionName));
105128

106129
var assemblyFinder = AssemblyFinder.ForSource(configurationAssemblySource);
107130

108-
return settingConfiguration.Settings(new ConfigurationReader(configuration.GetSection(DefaultSectionName), assemblyFinder, configuration));
131+
return settingConfiguration.Settings(new ConfigurationReader(configuration.GetSection(sectionName), assemblyFinder, configuration));
109132
}
110133

134+
/// <summary>
135+
/// Reads logger settings from the provided configuration object using the default section name. Generally this
136+
/// is preferable over the other method that takes a configuration section. Only this version will populate
137+
/// IConfiguration parameters on target methods.
138+
/// </summary>
139+
/// <param name="settingConfiguration">Logger setting configuration.</param>
140+
/// <param name="configuration">A configuration object which contains a Serilog section.</param>
141+
/// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
142+
/// <returns>An object allowing configuration to continue.</returns>
143+
public static LoggerConfiguration Configuration(
144+
this LoggerSettingsConfiguration settingConfiguration,
145+
IConfiguration configuration,
146+
ConfigurationAssemblySource configurationAssemblySource)
147+
=> Configuration(settingConfiguration, configuration, DefaultSectionName, configurationAssemblySource);
148+
111149
/// <summary>
112150
/// Reads logger settings from the provided configuration section. Generally it is preferable to use the other
113151
/// extension method that takes the full configuration object.
@@ -116,6 +154,7 @@ public static LoggerConfiguration Configuration(
116154
/// <param name="configSection">The Serilog configuration section</param>
117155
/// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
118156
/// <returns>An object allowing configuration to continue.</returns>
157+
[Obsolete("Use ReadFrom.Configuration(IConfiguration configuration, string sectionName, ConfigurationAssemblySource configurationAssemblySource) instead.")]
119158
public static LoggerConfiguration ConfigurationSection(
120159
this LoggerSettingsConfiguration settingConfiguration,
121160
IConfigurationSection configSection,

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public void ReadFromConfigurationSectionReadsFromAnArbitrarySection()
3535
.AddJsonString(json)
3636
.Build();
3737

38+
#pragma warning disable CS0618 // Type or member is obsolete
3839
var log = new LoggerConfiguration()
3940
.ReadFrom.ConfigurationSection(config.GetSection("NotSerilog"))
41+
#pragma warning restore CS0618 // Type or member is obsolete
4042
.WriteTo.Sink(new DelegatingSink(e => evt = e))
4143
.CreateLogger();
4244

@@ -65,8 +67,10 @@ public void ReadFromConfigurationSectionThrowsWhenTryingToCallConfigurationMetho
6567
.Build();
6668

6769
var exception = Assert.Throws<InvalidOperationException>(() =>
70+
#pragma warning disable CS0618 // Type or member is obsolete
6871
new LoggerConfiguration()
6972
.ReadFrom.ConfigurationSection(config.GetSection("NotSerilog"))
73+
#pragma warning restore CS0618 // Type or member is obsolete
7074
.CreateLogger());
7175

7276
Assert.Equal("Trying to invoke a configuration method accepting a `IConfiguration` argument. " +
@@ -76,6 +80,29 @@ public void ReadFromConfigurationSectionThrowsWhenTryingToCallConfigurationMetho
7680

7781
}
7882

83+
[Fact]
84+
public void ReadFromConfigurationDoesNotThrowWhenTryingToCallConfigurationMethodWithIConfigurationParam()
85+
{
86+
var json = @"{
87+
""NotSerilog"": {
88+
""Using"": [""TestDummies""],
89+
""WriteTo"": [{
90+
""Name"": ""DummyWithConfiguration"",
91+
""Args"": {}
92+
}]
93+
}
94+
}";
95+
96+
var config = new ConfigurationBuilder()
97+
.AddJsonString(json)
98+
.Build();
99+
100+
var exception = new LoggerConfiguration()
101+
.ReadFrom.Configuration(config, "NotSerilog")
102+
.CreateLogger();
103+
104+
}
105+
79106
[Fact]
80107
[Trait("BugFix", "https://github.com/serilog/serilog-settings-configuration/issues/143")]
81108
public void ReadFromConfigurationSectionDoesNotThrowWhenTryingToCallConfigurationMethodWithOptionalIConfigurationParam()
@@ -95,8 +122,10 @@ public void ReadFromConfigurationSectionDoesNotThrowWhenTryingToCallConfiguratio
95122
.Build();
96123

97124
// this should not throw because DummyWithOptionalConfiguration accepts an optional config
125+
#pragma warning disable CS0618 // Type or member is obsolete
98126
new LoggerConfiguration()
99127
.ReadFrom.ConfigurationSection(config.GetSection("NotSerilog"))
128+
#pragma warning restore CS0618 // Type or member is obsolete
100129
.CreateLogger();
101130

102131
}

0 commit comments

Comments
 (0)