Skip to content
This repository has been archived by the owner on Feb 27, 2022. It is now read-only.

Support for conditional sinks/enrichers #34

Merged
merged 2 commits into from
Dec 19, 2019

Conversation

nblumhardt
Copy link
Collaborator

From the tests:

[Fact]
public void ExpressionsControlConditionalSinks()
{
    var sink = new CollectingSink();
    var logger = new LoggerConfiguration()
        .WriteTo.Conditional("A = 1 or A = 2", wt => wt.Sink(sink))
        .CreateLogger();

    foreach (var a in Enumerable.Range(0, 5))
        logger.Information("{A}", a);

    Assert.Equal(2, sink.Events.Count);
}

[Fact]
public void ExpressionsControlConditionalEnrichment()
{
    var sink = new CollectingSink();
    var logger = new LoggerConfiguration()
        .Enrich.When("A = 1 or A = 2", e => e.WithProperty("B", 1))
        .WriteTo.Sink(sink)
        .CreateLogger();

    foreach (var a in Enumerable.Range(0, 5))
        logger.Information("{A}", a);

    Assert.Equal(2, sink.Events.Count(e => e.Properties.ContainsKey("B")));
}

Requires Serilog 2.9.

@nblumhardt nblumhardt merged commit a422ac0 into serilog-archive:dev Dec 19, 2019
@nblumhardt nblumhardt mentioned this pull request Dec 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant