Context, and what I tried so far...
As a policy our application configures logging from appsetting.json. I am using the following code:
// configuration variable is properly built with ConfigurationBuilder
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
When I try to add something new to the config (enricher, destructure, filter, etc) it is always hard time to do it, because the majority of samples, getting started's and documentations are using the fluent API of the LoggerConfiguration
class.
I could easily copy those examples + using the IDE intellisense to achieve my goal, but unfortunately I have to write a valid json configuration file, which is not always straightforward.
My idea was, create the configuration runtime in a POC project, then serialize somehow the built LoggerConfiguration
instance, so I will have a sample serilog json configuration file.
Unfortuanelly I can not find and inverse of the ReadFrom.Configuration(...)
operation.
I've also tried simply just serialize the built LoggerConfiguration
with the following code (using System.Text.Json
):
var loggerConfigurarion = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.WithThreadName()
.Enrich.WithProperty(ThreadNameEnricher.ThreadNamePropertyName, "Unknown");
var myHope = JsonSerializer.Serialize(loggerConfigurarion, new JsonSerializerOptions {WriteIndented = true});
but this results a mainly empty json (which is also a puzzler, how)
{
"WriteTo": {},
"AuditTo": {},
"MinimumLevel": {},
"Enrich": {},
"Filter": {},
"Destructure": {},
"ReadFrom": {}
}
Question
So the question remains: Is there any way to get a valid Serilog json configuration (string or file) from a LoggerConfiguration instance?