Skip to content

Commit d2ccdf7

Browse files
committed
Throw InvalidOperationException when IConfiguration is not available
but user tries to invoke a config method accepting a `IConfiguration`so that it is obvious that something is wrong. Probably accidentally fixes another issue
1 parent da3d6d8 commit d2ccdf7

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
@@ -331,7 +331,15 @@ static void CallConfigurationMethods(ILookup<string, Dictionary<string, IConfigu
331331
select directive.Key == null ? p.DefaultValue : directive.Value.ConvertTo(p.ParameterType, declaredLevelSwitches)).ToList();
332332

333333
var parm = methodInfo.GetParameters().FirstOrDefault(i => i.ParameterType == typeof(IConfiguration));
334-
if (parm != null) call[parm.Position - 1] = _configuration;
334+
if (parm != null)
335+
{
336+
if (_configuration is null)
337+
{
338+
throw new InvalidOperationException("Trying to invoke a configuration method accepting a `IConfiguration` argument. " +
339+
$"This is not supported when only a `IConfigSection` has been provided. (method '{methodInfo}')");
340+
}
341+
call[parm.Position - 1] = _configuration;
342+
}
335343

336344
call.Insert(0, receiver);
337345

@@ -348,7 +356,11 @@ internal static MethodInfo SelectConfigurationMethod(IEnumerable<MethodInfo> can
348356
return candidateMethods
349357
.Where(m => m.Name == name &&
350358
m.GetParameters().Skip(1)
351-
.All(p => p.HasDefaultValue || suppliedArgumentValues.Any(s => s.Key.Equals(p.Name, StringComparison.OrdinalIgnoreCase))))
359+
.All(p => p.HasDefaultValue
360+
|| suppliedArgumentValues.Any(s => s.Key.Equals(p.Name, StringComparison.OrdinalIgnoreCase))
361+
// parameters of type IConfiguration are implicitly populated with provided Configuration
362+
|| p.ParameterType == typeof(IConfiguration)
363+
))
352364
.OrderByDescending(m =>
353365
{
354366
var matchingArgs = m.GetParameters().Where(p => suppliedArgumentValues.Any(s => s.Key.Equals(p.Name, StringComparison.OrdinalIgnoreCase))).ToList();

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,38 @@ public void ReadFromConfigurationSectionReadsFromAnArbitrarySection()
4545
Assert.NotNull(evt);
4646
Assert.Equal("Test", evt.Properties["App"].LiteralValue());
4747
}
48+
49+
50+
51+
[Fact]
52+
[Trait("BugFix", "https://github.com/serilog/serilog-settings-configuration/issues/143")]
53+
public void ReadFromConfigurationSectionThrowsWhenTryingToCallConfigurationMethodWithIConfigurationParam()
54+
{
55+
var json = @"{
56+
""NotSerilog"": {
57+
""Using"": [""TestDummies""],
58+
""WriteTo"": [{
59+
""Name"": ""DummyRollingFile"",
60+
""Args"": {""pathFormat"" : ""C:\\"",
61+
""configurationSection"" : { ""foo"" : ""bar"" } }
62+
}]
63+
}
64+
}";
65+
66+
var config = new ConfigurationBuilder()
67+
.AddJsonString(json)
68+
.Build();
69+
70+
var exception = Assert.Throws<InvalidOperationException>(() =>
71+
new LoggerConfiguration()
72+
.ReadFrom.ConfigurationSection(config.GetSection("NotSerilog"))
73+
.CreateLogger());
74+
75+
Assert.Equal("Trying to invoke a configuration method accepting a `IConfiguration` argument. " +
76+
"This is not supported when only a `IConfigSection` has been provided. " +
77+
"(method 'Serilog.LoggerConfiguration DummyRollingFile(Serilog.Configuration.LoggerSinkConfiguration, Microsoft.Extensions.Configuration.IConfiguration, Microsoft.Extensions.Configuration.IConfigurationSection, System.String, Serilog.Events.LogEventLevel)')",
78+
exception.Message);
79+
80+
}
4881
}
4982
}

0 commit comments

Comments
 (0)