If like me you think Serilog is pretty fantastic then you might have wanted to extend your implementation with custom enrichers.
Below are a couple templates you can use to dynamically enrich your logs with whatever properties you need.
In my example I’m enriching my logs with the property ReleaseNumber given the environment variable RELEASE_NUMBER.
1. Create the log event enricher
This is the real meat when it comes to creating a custom Serilog enricher. The two important aspects are setting a constant PropertyName value
and providing a means to resolve the value of that property.
2. Provide a means to add your enricher to your logger configuration
3 Time to use your enricher!
There’s two ways we can go about using a custom enricher. First is probably the simplest and most commonly adopted way.
Along with any other setup, here we can add our own enricher to the configuration builder.
The alternative way is to import your enricher using an appsettings.json file using the Serilog.Settings.Configuration package.
This package will scan all assemblies listed in the Using property (as well as any assemblies belonging to the Serilog namespace)
for methods extending the LoggerEnrichmentConfiguration object.
If your enricher isn’t being applied then double check the Using property
matches the dll where your extension method resides.