g.pickardou
g.pickardou

Reputation: 35913

Is there any way to get a valid Serilog json configuration (string or file) from a properly built LoggerConfiguration instance?

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?

Upvotes: 0

Views: 1651

Answers (1)

C. Augusto Proiete
C. Augusto Proiete

Reputation: 27868

Update: There's community project called SerilogAnalyzer that can generate the contents of appSettings.json and app.config from a C# Serilog pipeline. It's super cool!

SerilogAnalyzer screenshot


There's nothing built-in into Serilog that allows you to do that, and you wouldn't be able to simply serialize the LogConfiguration to text the way you're thinking about it, because most of the configuration is not stored in simple variables, but instead in lambda functions that are created during the creation of the pipeline.

Either way, you'd have to resort to using Reflection to inspect any of the configured sinks and their properties. You can see a related example on this answer:

Unit test Serilog configuration

That said, I personally think using the fluent API is the way to go, instead of using the JSON config (or the XML app.config) because sooner or later you'll want to make some sinks asynchronous, and/or use a predicate or a function to configure certain aspects of the pipeline (for example, to configure a sub-logger), and it will very hard (and in some cases impossible) to configure in the JSON/XML config anyway.

You can use the options pattern to configure the small parts that you think you might want to change via configuration (such as URLs, logging levels, etc), and the rest of it, use the fluent API combined with conditional sinks.

Upvotes: 1

Related Questions