-
Notifications
You must be signed in to change notification settings - Fork 131
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
Are "WriteTo" sections additive or replacement when using multiple appsettings json files? #234
Comments
I think I'm having a related issue. I've built a helper assembly to apply the Serilog configuration the same way in all of our microservices. An extension method for the HostBuilder uses the Assembly assembly = typeof(ConfigurationBuilderExtensions).Assembly;
configurationBuilder.AddJsonStream(assembly.GetManifestResourceStream($"{typeof(ConfigurationBuilderExtensions).Namespace}.serilog.configuration.web.json"));
Stream stream = assembly.GetManifestResourceStream($"{typeof(ConfigurationBuilderExtensions).Namespace}.serilog.configuration.web.{(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production").ToLowerInvariant()}.json");
if (stream != null)
{
configurationBuilder.AddJsonStream(stream);
}
return configurationBuilder
.AddJsonFile(Path.Combine(contentRootPath, "serilog.configuration.web.json"), optional: true, reloadOnChange: true)
.AddJsonFile(Path.Combine(contentRootPath, $"serilog.configuration.web.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json"),
optional: true, reloadOnChange: true)
.AddEnvironmentVariables(); This should give me a hierarchical set of configuration where I can define in the embedded JSON a {
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Console"
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
}
}
]
}
} In my testing app I can then include a file {
"Serilog": {
"MinimumLevel": {
"Default": "Verbose"
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
]
}
} Based on my understanding of how the configuration builder works this would give me an Testing this: .UseSerilog((context, serviceProvider, loggerConfiguration) =>
{
var formatterVal = context.Configuration.GetValue<string>("Serilog:WriteTo:0:Args:formatter")
loggerConfiguration
.ReadFrom.Configuration(context.Configuration)
.Enrich.FromLogContext();
}) The This is expected. However, the console formatter at runtime is the standard console logger with the "AnsiConsoleTheme" enabled. I did some digging in the code and found the I'm not sure if this is expected behavior, some oddity with how the configuration works, or something else. |
Wild guess, but I think the problem might be that you are defining your sinks as an array in Therefore, if you have this
it would be equivalent as having this
Then, in your You could try defining your WriteTo using names instead of the index as explained in the Readme and the sample. |
Could be closed? |
I'd like to expand a bit on this with a similar scenario that also causes some pain points, if I could. It may be appropriate as a new issue but they seem similar enough If you define a "MinimumLevel": "Information" Then attempt to override that configuration in another JSON file (e.g. "MinimumLevel": {
"Default": "Debug"
} The second value is ignored and the minimum level will be strictly "Information". Ideally, the library could detect duplicate Worth noting also that the Overrides are still respected, it's just the |
When using multiple appsettings.json files, the "WriteTo" section appears to a full replacement by the overriding appsetting json file.
Sample config / setup...
Program
appsettings.serilog.json
appsettings.serilog.Development.json
Current Behavior (when running under .net core development environment)
Maybe Expected Behavior?
WriteTo
sections should be addedSummary
I think the general question can be resolved with: How can we define a sink (
WriteTo
) so that it is only defined in one appsetting file and is picked up by the overriding appsetting config json file? Or do we need to define duplicate sinks in each appsetting for each environment we want the sink to be included?After writing this, I am about 99% sure this is expected behavior for the overriding appsetting json to overwrite the entire
WriteTo
section.The text was updated successfully, but these errors were encountered: