Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
No longer throw when calling a method with optional IConfiguration param
In that case, the default value of the param will be passed
  • Loading branch information
tsimbalar committed Oct 16, 2018
1 parent dc53ec3 commit 7ead54e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
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
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
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();

}
}
}

0 comments on commit 7ead54e

Please sign in to comment.