Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

A filter for reducing duplicate Serilog log entries

License

Notifications You must be signed in to change notification settings

waxtell/Serilog.Aggregating.Filter

Repository files navigation

Serilog.Aggregating.Filter Build status

Aggregation-based event filtering for Serilog.

const string expr = "@Level = 'Information' and AppId is not null and Items[?] like 'C%'";

Log.Logger = new LoggerConfiguration()
                .Enrich.WithProperty("AppId", 10)
                .Filter.UniqueOverSpan(expr, TimeSpan.FromSeconds(20))
                .WriteTo.Console()
                .CreateLogger();

// Printed
Log.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });

// Not printed (duplicate message)
Log.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });

//Printed (distinct message)
Log.Information("Cart contains {@Items}", new[] { "Peanuts", "Chocolate" });

// Printed (excluded from filter due to log level)
Log.Warning("Cart contains {@Items}", new[] { "Tea", "Coffee" });

Log.CloseAndFlush();

Getting started

Install Serilog.Aggregating.Filter from NuGet:

Install-Package Serilog.Aggregating.Filter

Please see Serilog.Filters.Expressions for filter syntax.

JSON appSettings.json configuration

Using Serilog.Settings.Configuration:

{
  "Serilog": {
    "Using": [
        "Serilog.Settings.Configuration",
        "Serilog.Aggregating.Filter"
    ],
    "Filter": [
      {
        "Name": "UniqueOverSpan",
        "Args": {
            "expression": "@Level = 'Information' and Items[?] like 'C%'",
            "span": "00:00:20"

        }
      }
    ]

About

A filter for reducing duplicate Serilog log entries

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages