Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration from contents of Serilog section #362

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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