Skip to content

Added new ReadFrom.Configuration(...) overloads; Marked old as obsolete (#148) #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,52 @@ public static class ConfigurationLoggerConfigurationExtensions
public const string DefaultSectionName = "Serilog";

/// <summary>
/// Reads logger settings from the provided configuration object using the default section name. Generally this
/// Reads logger settings from the provided configuration object using the provided section name. Generally this
/// is preferable over the other method that takes a configuration section. Only this version will populate
/// IConfiguration parameters on target methods.
/// </summary>
/// <param name="settingConfiguration">Logger setting configuration.</param>
/// <param name="configuration">A configuration object which contains a Serilog section.</param>
/// <param name="sectionName">A section name for section which contains a Serilog section.</param>
/// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform
/// default will be used.</param>
/// <returns>An object allowing configuration to continue.</returns>
public static LoggerConfiguration Configuration(
this LoggerSettingsConfiguration settingConfiguration,
IConfiguration configuration,
string sectionName,
DependencyContext dependencyContext = null)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
if (sectionName == null) throw new ArgumentNullException(nameof(sectionName));

var assemblyFinder = dependencyContext == null
? AssemblyFinder.Auto()
: AssemblyFinder.ForDependencyContext(dependencyContext);

return settingConfiguration.Settings(
new ConfigurationReader(
configuration.GetSection(DefaultSectionName),
configuration.GetSection(sectionName),
assemblyFinder,
configuration));
}

/// <summary>
/// Reads logger settings from the provided configuration object using the default section name. Generally this
/// is preferable over the other method that takes a configuration section. Only this version will populate
/// IConfiguration parameters on target methods.
/// </summary>
/// <param name="settingConfiguration">Logger setting configuration.</param>
/// <param name="configuration">A configuration object which contains a Serilog section.</param>
/// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform
/// default will be used.</param>
/// <returns>An object allowing configuration to continue.</returns>
public static LoggerConfiguration Configuration(
this LoggerSettingsConfiguration settingConfiguration,
IConfiguration configuration,
DependencyContext dependencyContext = null)
=> Configuration(settingConfiguration, configuration, DefaultSectionName, dependencyContext);

/// <summary>
/// Reads logger settings from the provided configuration section. Generally it is preferable to use the other
/// extension method that takes the full configuration object.
Expand All @@ -68,6 +87,7 @@ public static LoggerConfiguration Configuration(
/// <param name="dependencyContext">The dependency context from which sink/enricher packages can be located. If not supplied, the platform
/// default will be used.</param>
/// <returns>An object allowing configuration to continue.</returns>
[Obsolete("Use ReadFrom.Configuration(IConfiguration configuration, string sectionName, DependencyContext dependencyContext) instead.")]
public static LoggerConfiguration ConfigurationSection(
this LoggerSettingsConfiguration settingConfiguration,
IConfigurationSection configSection,
Expand All @@ -88,26 +108,44 @@ public static LoggerConfiguration ConfigurationSection(
}

/// <summary>
/// Reads logger settings from the provided configuration object using the default section name. Generally this
/// Reads logger settings from the provided configuration object using the provided section name. Generally this
/// is preferable over the other method that takes a configuration section. Only this version will populate
/// IConfiguration parameters on target methods.
/// </summary>
/// <param name="settingConfiguration">Logger setting configuration.</param>
/// <param name="configuration">A configuration object which contains a Serilog section.</param>
/// <param name="sectionName">A section name for section which contains a Serilog section.</param>
/// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
/// <returns>An object allowing configuration to continue.</returns>
public static LoggerConfiguration Configuration(
this LoggerSettingsConfiguration settingConfiguration,
IConfiguration configuration,
string sectionName,
ConfigurationAssemblySource configurationAssemblySource)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
if (sectionName == null) throw new ArgumentNullException(nameof(sectionName));

var assemblyFinder = AssemblyFinder.ForSource(configurationAssemblySource);

return settingConfiguration.Settings(new ConfigurationReader(configuration.GetSection(DefaultSectionName), assemblyFinder, configuration));
return settingConfiguration.Settings(new ConfigurationReader(configuration.GetSection(sectionName), assemblyFinder, configuration));
}

/// <summary>
/// Reads logger settings from the provided configuration object using the default section name. Generally this
/// is preferable over the other method that takes a configuration section. Only this version will populate
/// IConfiguration parameters on target methods.
/// </summary>
/// <param name="settingConfiguration">Logger setting configuration.</param>
/// <param name="configuration">A configuration object which contains a Serilog section.</param>
/// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
/// <returns>An object allowing configuration to continue.</returns>
public static LoggerConfiguration Configuration(
this LoggerSettingsConfiguration settingConfiguration,
IConfiguration configuration,
ConfigurationAssemblySource configurationAssemblySource)
=> Configuration(settingConfiguration, configuration, DefaultSectionName, configurationAssemblySource);

/// <summary>
/// Reads logger settings from the provided configuration section. Generally it is preferable to use the other
/// extension method that takes the full configuration object.
Expand All @@ -116,6 +154,7 @@ public static LoggerConfiguration Configuration(
/// <param name="configSection">The Serilog configuration section</param>
/// <param name="configurationAssemblySource">Defines how the package identifies assemblies to scan for sinks and other Types.</param>
/// <returns>An object allowing configuration to continue.</returns>
[Obsolete("Use ReadFrom.Configuration(IConfiguration configuration, string sectionName, ConfigurationAssemblySource configurationAssemblySource) instead.")]
public static LoggerConfiguration ConfigurationSection(
this LoggerSettingsConfiguration settingConfiguration,
IConfigurationSection configSection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public void ReadFromConfigurationSectionReadsFromAnArbitrarySection()
.AddJsonString(json)
.Build();

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

Expand Down Expand Up @@ -65,8 +67,10 @@ public void ReadFromConfigurationSectionThrowsWhenTryingToCallConfigurationMetho
.Build();

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

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

}

[Fact]
public void ReadFromConfigurationDoesNotThrowWhenTryingToCallConfigurationMethodWithIConfigurationParam()
{
var json = @"{
""NotSerilog"": {
""Using"": [""TestDummies""],
""WriteTo"": [{
""Name"": ""DummyWithConfiguration"",
""Args"": {}
}]
}
}";

var config = new ConfigurationBuilder()
.AddJsonString(json)
.Build();

var exception = new LoggerConfiguration()
.ReadFrom.Configuration(config, "NotSerilog")
.CreateLogger();

}

[Fact]
[Trait("BugFix", "https://github.com/serilog/serilog-settings-configuration/issues/143")]
public void ReadFromConfigurationSectionDoesNotThrowWhenTryingToCallConfigurationMethodWithOptionalIConfigurationParam()
Expand All @@ -95,8 +122,10 @@ public void ReadFromConfigurationSectionDoesNotThrowWhenTryingToCallConfiguratio
.Build();

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

}
Expand Down