Skip to content

Commit 0bc3c2c

Browse files
committed
Do not access "Serilog" sub-section when calling ReadFrom.ConfigSection
fixes #143 where we are trying to read a subsection of config section passed as parameter
1 parent f919606 commit 0bc3c2c

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public ConfigurationReader(IConfiguration configuration, DependencyContext depen
3535

3636
// Generally the initial call should use IConfiguration rather than IConfigurationSection, otherwise
3737
// IConfiguration parameters in the target methods will not be populated.
38-
ConfigurationReader(IConfigurationSection configSection, DependencyContext dependencyContext)
38+
public ConfigurationReader(IConfigurationSection configSection, DependencyContext dependencyContext)
3939
{
40-
_section = configSection ?? throw new ArgumentNullException(nameof(configSection));
40+
_configuration = configSection ?? throw new ArgumentNullException(nameof(configSection));
41+
_section = configSection;
4142
_dependencyContext = dependencyContext;
4243
_configurationAssemblies = LoadConfigurationAssemblies();
4344
}

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System;
1+
using System;
22
using Microsoft.Extensions.Configuration;
3+
using Serilog.Events;
4+
using Serilog.Settings.Configuration.Tests.Support;
35
using Xunit;
46

57
namespace Serilog.Settings.Configuration.Tests
@@ -14,5 +16,34 @@ public void ReadFromConfigurationShouldNotThrowOnEmptyConfiguration()
1416
// should not throw
1517
act();
1618
}
19+
20+
[Fact]
21+
[Trait("BugFix", "https://github.com/serilog/serilog-settings-configuration/issues/143")]
22+
public void ReadFromConfigurationSectionReadsFromAnArbitrarySection()
23+
{
24+
LogEvent evt = null;
25+
26+
var json = @"{
27+
""NotSerilog"": {
28+
""Properties"": {
29+
""App"": ""Test""
30+
}
31+
}
32+
}";
33+
34+
var config = new ConfigurationBuilder()
35+
.AddJsonString(json)
36+
.Build();
37+
38+
var log = new LoggerConfiguration()
39+
.ReadFrom.ConfigurationSection(config.GetSection("NotSerilog"))
40+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
41+
.CreateLogger();
42+
43+
log.Information("Has a test property");
44+
45+
Assert.NotNull(evt);
46+
Assert.Equal("Test", evt.Properties["App"].LiteralValue());
47+
}
1748
}
1849
}

0 commit comments

Comments
 (0)