Skip to content

No longer throw when calling a method with optional IConfiguration param #149

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
Oct 17, 2018
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 @@ -331,7 +331,7 @@ static void CallConfigurationMethods(ILookup<string, Dictionary<string, IConfigu
select directive.Key == null ? p.DefaultValue : directive.Value.ConvertTo(p.ParameterType, declaredLevelSwitches)).ToList();

var parm = methodInfo.GetParameters().FirstOrDefault(i => i.ParameterType == typeof(IConfiguration));
if (parm != null)
if (parm != null && !parm.HasDefaultValue)
{
if (_configuration is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ public void SinkWithOptionalIConfigurationArguments()

log.Write(Some.InformationEvent());

Assert.NotNull(DummyConfigurationSink.Configuration);
// null is the default value
Assert.Null(DummyConfigurationSink.Configuration);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public void ReadFromConfigurationSectionThrowsWhenTryingToCallConfigurationMetho
""Using"": [""TestDummies""],
""WriteTo"": [{
""Name"": ""DummyWithConfiguration"",
""Args"": {""pathFormat"" : ""C:\\"",
""configurationSection"" : { ""foo"" : ""bar"" } }
""Args"": {}
}]
}
}";
Expand All @@ -76,5 +75,30 @@ public void ReadFromConfigurationSectionThrowsWhenTryingToCallConfigurationMetho
exception.Message);

}

[Fact]
[Trait("BugFix", "https://github.com/serilog/serilog-settings-configuration/issues/143")]
public void ReadFromConfigurationSectionDoesNotThrowWhenTryingToCallConfigurationMethodWithOptionalIConfigurationParam()
{
var json = @"{
""NotSerilog"": {
""Using"": [""TestDummies""],
""WriteTo"": [{
""Name"": ""DummyWithOptionalConfiguration"",
""Args"": {}
}]
}
}";

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

// this should not throw because DummyWithOptionalConfiguration accepts an optional config
new LoggerConfiguration()
.ReadFrom.ConfigurationSection(config.GetSection("NotSerilog"))
.CreateLogger();

}
}
}