Open LordMike opened 2 years ago
Microsoft.Extensions.Configuration is a bit tricky at times :thinking:
Using Serilog.Expressions and an expression template, through the formatter
argument:
https://github.com/serilog/serilog-settings-configuration/pull/281
will let you switch between JSON and plain text output by modifying the same configuration settings (formatter.template
).
HTH!
I have this issue too and wanted to avoid Serilog.Expressions, any solution?
From what I read, Microsoft.Extensions.Configuration doesn't support unsetting values.
Indeed, Enable removal of a key or subtree was filed on Oct 16, 2015 and is still unaddressed as of today. And with only 6 thumbs up, I doubt a 7th will have any impact. 🙁
The problem is that Serilog.Settings.Configuration
must choose which method to call given a set of parameters. For the Console
sink, when given outputTemplate = ""
and formatter = "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
the method that takes the output template is selected because there's one matching argument for each but outputTemplate
is a string and formatter
is not:
var matchingArgs = m.GetParameters().Where(p => ParameterNameMatches(p.Name, suppliedArgumentNames)).ToList();
// Prefer the configuration method with most number of matching arguments and of those the ones with
// the most string type parameters to predict best match with least type casting
return new Tuple<int, int>(
matchingArgs.Count,
matchingArgs.Count(p => p.ParameterType == typeof(string)));
Theoretically it could be possible to make Serilog.Settings.Configuration
choose the method that takes the ITextFormatter
by specifying one more matching parameter but both methods have the same parameters (restrictedToMinimumLevel
, levelSwitch
and standardErrorFromLevel
)
So your best chance is probably to use Serilog.Expressions
as proposed by @nblumhardt.
I'm trying to make an app that has a default console output format (With an
OutputTemplate
), but is otherwise overridable by configs (environment variables in my case).I've made an example app to show my issue. I have a config like this:
I can then use it:
Which outputs
So far so good. Now I want my users to use environment variables (or something else) to override the configs, and potentially use a custom formatter, like
RenderedCompactJsonFormatter
. So we add in environment variables to our config builder, and add the following:But this doesn't override the OutputTemplate, it still outputs
My custom template
- if I remove theOutputTemplate
from the config json (using the default from the Console sink), the environment variable works - it switches to json rendering.I've also tried overriding the template with a blank string, like:
.. but this just changes the output template to a blank string - it's still used.
From what I read, Microsoft.Extensions.Configuration doesn't support unsetting values, you cannot f.ex. configure a specific key to null, and have it removed from the system. The closest you can get, is overriding a value with a blank string, and have that be a convention to be "unset". For example, this blog post also highlights the issue:
Are there any tips on how I can provide a custom default template, but let users override my config using Microsofts configuration system?
String.Empty
, and consider it "null" ?I think I'm going with skipping the custom format by default, in order to let users override the formatter - but I think there might be a general issue here :).