diff --git a/src/Serilog.Settings.Configuration/ConfigurationLoggerConfigurationExtensions.cs b/src/Serilog.Settings.Configuration/ConfigurationLoggerConfigurationExtensions.cs index 1e99aa40..b4ce0910 100644 --- a/src/Serilog.Settings.Configuration/ConfigurationLoggerConfigurationExtensions.cs +++ b/src/Serilog.Settings.Configuration/ConfigurationLoggerConfigurationExtensions.cs @@ -228,20 +228,20 @@ public static LoggerConfiguration Configuration( static ConfigurationReader GetConfigurationReader(IConfiguration configuration, ConfigurationReaderOptions readerOptions, DependencyContext dependencyContext) { var assemblyFinder = dependencyContext == null ? AssemblyFinder.Auto() : AssemblyFinder.ForDependencyContext(dependencyContext); - var section = configuration.GetSection(readerOptions.SectionName); + var section = string.IsNullOrWhiteSpace(readerOptions.SectionName) ? configuration : configuration.GetSection(readerOptions.SectionName); return new ConfigurationReader(section, assemblyFinder, readerOptions, configuration); } static ConfigurationReader GetConfigurationReader(IConfiguration configuration, ConfigurationReaderOptions readerOptions, ConfigurationAssemblySource source) { var assemblyFinder = AssemblyFinder.ForSource(source); - var section = configuration.GetSection(readerOptions.SectionName); + var section = string.IsNullOrWhiteSpace(readerOptions.SectionName) ? configuration : configuration.GetSection(readerOptions.SectionName); return new ConfigurationReader(section, assemblyFinder, readerOptions, configuration); } static ConfigurationReader GetConfigurationReader(IConfiguration configuration, ConfigurationReaderOptions readerOptions, IReadOnlyCollection assemblies) { - var section = configuration.GetSection(readerOptions.SectionName); + var section = string.IsNullOrWhiteSpace(readerOptions.SectionName) ? configuration : configuration.GetSection(readerOptions.SectionName); return new ConfigurationReader(section, assemblies, new ResolutionContext(configuration, readerOptions)); } } diff --git a/src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs b/src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs index f0202f27..8d166359 100644 --- a/src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs +++ b/src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs @@ -17,12 +17,12 @@ class ConfigurationReader : IConfigurationReader { const string LevelSwitchNameRegex = @"^\${0,1}[A-Za-z]+[A-Za-z0-9]*$"; - readonly IConfigurationSection _section; + readonly IConfiguration _section; readonly IReadOnlyCollection _configurationAssemblies; readonly ResolutionContext _resolutionContext; readonly IConfigurationRoot _configurationRoot; - public ConfigurationReader(IConfigurationSection configSection, AssemblyFinder assemblyFinder, ConfigurationReaderOptions readerOptions, IConfiguration configuration = null) + public ConfigurationReader(IConfiguration configSection, AssemblyFinder assemblyFinder, ConfigurationReaderOptions readerOptions, IConfiguration configuration = null) { _section = configSection ?? throw new ArgumentNullException(nameof(configSection)); _configurationAssemblies = LoadConfigurationAssemblies(_section, assemblyFinder); @@ -31,7 +31,7 @@ public ConfigurationReader(IConfigurationSection configSection, AssemblyFinder a } // Used internally for processing nested configuration sections -- see GetMethodCalls below. - internal ConfigurationReader(IConfigurationSection configSection, IReadOnlyCollection configurationAssemblies, ResolutionContext resolutionContext) + internal ConfigurationReader(IConfiguration configSection, IReadOnlyCollection configurationAssemblies, ResolutionContext resolutionContext) { _section = configSection ?? throw new ArgumentNullException(nameof(configSection)); _configurationAssemblies = configurationAssemblies ?? throw new ArgumentNullException(nameof(configurationAssemblies)); @@ -298,7 +298,7 @@ void ApplyEnrichment(LoggerConfiguration loggerConfiguration) } } - internal ILookup> GetMethodCalls(IConfigurationSection directive) + internal ILookup> GetMethodCalls(IConfiguration directive) { var children = directive.GetChildren().ToList(); @@ -356,7 +356,7 @@ internal static IConfigurationArgumentValue GetArgumentValue(IConfigurationSecti return argumentValue; } - static IReadOnlyCollection LoadConfigurationAssemblies(IConfigurationSection section, AssemblyFinder assemblyFinder) + static IReadOnlyCollection LoadConfigurationAssemblies(IConfiguration section, AssemblyFinder assemblyFinder) { var serilogAssembly = typeof(ILogger).Assembly; var assemblies = new Dictionary { [serilogAssembly.FullName] = serilogAssembly }; diff --git a/test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs b/test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs index fbeeaf0c..5b4414c9 100644 --- a/test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs +++ b/test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs @@ -56,6 +56,31 @@ public void PropertyEnrichmentIsApplied() Assert.Equal("Test", evt.Properties["App"].LiteralValue()); } + [Theory] + [InlineData(null)] + [InlineData("")] + public void CanReadWithoutSerilogSection(string sectionName) + { + LogEvent evt = null; + + var json = """ + { + "Properties": { + "App": "Test" + } + } + """; + + var log = ConfigFromJson(json, options: new ConfigurationReaderOptions { SectionName = sectionName }) + .WriteTo.Sink(new DelegatingSink(e => evt = e)) + .CreateLogger(); + + log.Information("Has a test property"); + + Assert.NotNull(evt); + Assert.Equal("Test", evt.Properties["App"].LiteralValue()); + } + [Theory] [InlineData("extended syntax", """ {