简体   繁体   中英

Specifying a custom enricher using Serilog XML configuration

There are a lot of examples of configuring sinks and their properties using app settings configuration. However, I can't really wrap my head around configuring a custom enricher via app settings. Can this be done? I've tried to specify the configuration using the full path to my class and assembly's name, but it doesn't seem to work. Here's an example of the configuration I've tried to use:

<add key="serilog:enrich:with" value="MyApp.Logging.Serilog.MyEnricher, MyApp" />

The key-value pair syntax currently needs an extension method defined for this case to work, eg:

static class MyLoggerEnrichmentConfigurationExtensions
{
    public static LoggerConfiguration WithMyEnricher(this LoggerEnrichmentConfiguration enrich)
    {
        return enrich.With(new MyEnricher());
    }
}

It's then referenced and called like so:

<add key="serilog:using:MyApp" value="MyApp" />
<add key="serilog:enrich:WithMyEnricher" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM