I use ASP.NET Core json logging from Microsoft.Extensions.Logging
. I can configure encoder in code like this:
builder.Logging.AddJsonConsole(config =>
{
config.JsonWriterOptions = new JsonWriterOptions { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
});
But I don't understand why it doesn't work when I try to do the same thing by using only config:
{
"Logging": {
"Console": {
"FormatterOptions": {
"JsonWriterOptions": {
"Encoder": "System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping"
}
}
}
}
}
I believe you cannot set
Encoder
property this way because the defaultBind
implementation which bindsappsetting.json
values to an object doesn't bind static properties or anything else other than plain/collection values. A custom logger provider might be useful in this case.You can however set other
JsonWriterOptions
properties and you need to make sure to setFormatterName
tojson