Skip to content

Commit

Permalink
Merge pull request #362 from sungam3r/section
Browse files Browse the repository at this point in the history
Allow configuration from contents of Serilog section
  • Loading branch information
skomis-mm authored Mar 13, 2023
2 parents 679ce35 + 058cb9d commit 8736256
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Assembly> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Assembly> _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);
Expand All @@ -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<Assembly> configurationAssemblies, ResolutionContext resolutionContext)
internal ConfigurationReader(IConfiguration configSection, IReadOnlyCollection<Assembly> configurationAssemblies, ResolutionContext resolutionContext)
{
_section = configSection ?? throw new ArgumentNullException(nameof(configSection));
_configurationAssemblies = configurationAssemblies ?? throw new ArgumentNullException(nameof(configurationAssemblies));
Expand Down Expand Up @@ -298,7 +298,7 @@ void ApplyEnrichment(LoggerConfiguration loggerConfiguration)
}
}

internal ILookup<string, Dictionary<string, IConfigurationArgumentValue>> GetMethodCalls(IConfigurationSection directive)
internal ILookup<string, Dictionary<string, IConfigurationArgumentValue>> GetMethodCalls(IConfiguration directive)
{
var children = directive.GetChildren().ToList();

Expand Down Expand Up @@ -356,7 +356,7 @@ internal static IConfigurationArgumentValue GetArgumentValue(IConfigurationSecti
return argumentValue;
}

static IReadOnlyCollection<Assembly> LoadConfigurationAssemblies(IConfigurationSection section, AssemblyFinder assemblyFinder)
static IReadOnlyCollection<Assembly> LoadConfigurationAssemblies(IConfiguration section, AssemblyFinder assemblyFinder)
{
var serilogAssembly = typeof(ILogger).Assembly;
var assemblies = new Dictionary<string, Assembly> { [serilogAssembly.FullName] = serilogAssembly };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", """
{
Expand Down

0 comments on commit 8736256

Please sign in to comment.