0

I'm trying to write a series of custom enrichers whose values are set dynamically at run-time. In other words, they are not just looking up "static" values from the environment being logged.

To make that work I need to be able to determine which enrichers are present in a given Serilog configuration so that I can write values to them. I've been unable to find a way to list the enrichers that are present. Is it possible to do so?

If there's a different way of being able to inject values dynamically into the pipeline I'd be interested in hearing about it. I've built something to do that using LogContext but I'm hoping to simplify things (which is why I was looking into using enrichers).

Mark Olbert
  • 6,584
  • 9
  • 35
  • 69
  • Why do you need to determine which enrichers are present? – C. Augusto Proiete Aug 23 '21 at 13:35
  • Because I was planning on updating the enrichers dynamically at runtime. However, through experimentation I determined that enrichers do not support dynamic update -- they get created once, after which whatever property values they define are "static". So I've switched to a FromLogContext() approach, which does allow dynamic log event property setting. – Mark Olbert Aug 24 '21 at 00:14
  • It's definitely possible to create enrichers that have dynamic properties, there are a few examples in the [Serilog-Contrib org](https://github.com/serilog-contrib). In fact, [the `LogContext` itself is an enricher](https://github.com/serilog/serilog/blob/dev/src/Serilog/Context/LogContextEnricher.cs#L20) so if you can do what you need with `LogContext`, I see no reason why you wouldn't be able to do with custom enricher... See also: [Serilog.Enrichers.GlobalLogContext](https://github.com/serilog-contrib/serilog-enrichers-globallogcontext) – C. Augusto Proiete Aug 24 '21 at 01:31
  • I may go back and re-read those references when I have the time, thanx. When I said "static" I meant the enrichers I originally built didn't seem to get re-generated on each logging event. So what I did instead was manually push the contents of my custom enrichers at each log event to the LogContext and then delete them after calling Write(). I was hoping for a more auto-magic approach...which I may have missed in the docs :) – Mark Olbert Aug 24 '21 at 23:55

1 Answers1

1

Serilog does not expose a way to get the list of Enrichers that have been configured, at runtime. You'd have to create this list yourself at the start of the app, based on the log pipeline code and/or reading the configuration settings.

Alternatively, you could use Reflection if you really want to get this information from the live Serilog configuration, and if you're OK with it possibly breaking in the future as new Serilog versions are released and their internals change.

You can see an example of getting a list of sinks via Reflection on this answer:

Unit test Serilog configuration

Getting a list of Enrichers would be a similar approach.

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