Skip to content

ChangemakerStudios/Serilog.Enricher.WhenDo

Repository files navigation

Serilog.Enricher.WhenDo

NuGet version Build Status

Serilog extra that adds a fluent API to configure rules for modifying properties on the fly and piping events to secondary loggers.

Usage

Property Modification

Add/Remove/Update properties based on a criteria.

var log = 
    new LoggerConfiguration()
        .WriteTo.Trace()
        .Enrich.With<HttpRequestEnricher>()
        // We need to remove the RawUrl property if there is a payment
        // processing error otherwise we may expose the credit card in the logs.
        .When().IsExceptionOf<CreditCardPaymentException>().Do().RemovePropertyIfPresent("RawUrl")
        // When the the Special Service fails, log the current endpoint
        .When().IsExceptionOf<SpecialServiceException>().Do().AddOrUpdateProperty("SpecialServiceEndpoint", _settings.SpecialServiceEndpoint, true)
        // If one of the two possible properties is there, remove "UnnecessaryProperty"
        .When().HasProperty("PossibleProperty", "PossiblePropertyOther").Do().RemovePropertyIfPresent("UnnecessaryProperty")
        .CreateLogger();
Send/Pipe to Another Logger

SendTo sends a copy of an event to another logger if the critiera matches. PipeTo only sends the event to another logger if the criteria matches.

var backupLogger = new LoggerConfiguration().WriteTo.Trace.CreateLogger();

var specialLogger = new LoggerConfiguration().WriteTo.SpecialSink().CreateLogger();

var log = 
    new LoggerConfiguration()
        .WriteTo.Trace()
        // Sends a copy of the event to the backupLogger --
        // both loggers will get the same event.
        .When().FromSourceContext<AccountingService>().Do().SendTo(backupLogger)
        // Pipes all events that match criteria to the specialLogger.
        // "this" logger will not receive the event.
        .When().FromSourceContext<ChattyService>().Do().PipeTo(specialLogger)
        .CreateLogger();

Icon

Copyright "Solution" by Arthur Shlain from the Noun Project

About

Encricher/Filter Combo for Serilog that support property manipulation and filter as well as secondary logger routing/redirection

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages