0

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?

g.pickardou
  • 32,346
  • 36
  • 123
  • 268
  • None that I know of, however if you wrote one thad be great. The problem comes in due to how it reads json, as far as I know there is a bit of runtime tomfoolery it uses to resolve the parameters of a plugin ect at runtime and pumps those parameters in – TheGeneral Jan 14 '21 at 10:29
  • Many thx for answer. Good to know I am not alone with a geek idea. It seems they overengineered something, but having the plugins + the heavy extensions method usage + reflection it is hard to see. Those things for example statics or reflection always punch back sooner or later, we have to pay the price. – g.pickardou Jan 14 '21 at 12:16
  • Yeah, its not an impossible task, from what I know of how this works its just a simple reflection resolution pattern, and truthfully the configuration is like roulette where you just have to try and watch it not work, or make your own extensions. It would be a good thing – TheGeneral Jan 14 '21 at 12:48
  • @g.pickardou See my updated answer about the [`SerilogAnalyzer`](https://stackoverflow.com/a/65855852/211672) that can generate the `appSettings.json` from C# code – C. Augusto Proiete Jan 29 '21 at 17:40

1 Answers1

1

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.

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91