Serilog- grouping custom enrichers

Viewed 19

I created custom enrichers and registered them in Serilog. I don't want to register every enriched to appsetting.json. My question is how can I group this custom registers and only add this group name to appsettings.json

UserNameEnricher.cs

public class UserNameEnricher: ILogEventEnricher
    {
        public void Enrich(LogEvent logEvent, ILogEventPropertyFactory pf)
        {
            var propertyName = "UserName";
            var propertyValue = "HUZEYFE";
            var newProperty = pf.CreateProperty(propertyName, propertyValue);
            logEvent.AddPropertyIfAbsent(newProperty);
        }
    }

AddDateTimeNowEnricher.cs

public class AddDateTimeNowEnricher: ILogEventEnricher
    {
        public void Enrich(LogEvent logEvent, ILogEventPropertyFactory pf)
        {
            var propertyName = "DateTimeNow";
            var propertyValue = DateTime.Now;
            var newProperty = pf.CreateProperty(propertyName, propertyValue);
            logEvent.AddPropertyIfAbsent(newProperty);
        }
    }

And this is enricher registerer

public static class LoggingExtensions
    {
        public static LoggerConfiguration WithAddDateTimeNow(
            this LoggerEnrichmentConfiguration enrich)
        {
            if (enrich == null)
                throw new ArgumentNullException(nameof(enrich));

            return enrich.With<AddDateTimeNowEnricher>();

        }

        public static LoggerConfiguration WithUserName(
            this LoggerEnrichmentConfiguration enrich)
        {
            if (enrich == null)
                throw new ArgumentNullException(nameof(enrich));

            return enrich.With<UserNameEnricher>();

        }
    }
0 Answers
Related