jsonasp.net-coreconfiguration.net-8.0serilog

Filter out specific logs with Serilog.Expressions


I'm using Quartz.NET in my .NET web app and it floods the logs with the "Batch acquisition of # triggers" which I want to filter out from the logs. I managed to make this work with the deprecated Nuget package Serilog.Filters.Expressions 2.1.0 but now want to have the same result with the new Serilog.Expressions 5.0.0. Unfortunately, the same config in appsettings.json does not work. What should I fix here?

    {
      "Serilog": {
        "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Expressions" ],
        "MinimumLevel": {
          "Default": "Debug",
          "Override": {
            "Microsoft": "Warning",
            "System": "Warning"
          }
        },
        "Filter": [
          {
            "Name": "ByExcluding",
            "Args": {
              "expression": "Contains(@Message, 'Batch acquisition of')"
            }
          }
        ],
        "WriteTo": [
          { "Name": "Console" },
          {
            "Name": "File",
            "Args": {
              "path": "Logs/log-.txt",
              "rollingInterval": "Day"
            }
          }
        ],
        "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
      }
    }

Solution

  • Check out the Language reference:properties of the github docs. You can use @mt or @m:

    Properties

    The following properties are available in expressions:

    • All first-class properties of the event - no special syntax: SourceContext and Cart are used in the formatting examples above
    • @t - the event's timestamp, as a DateTimeOffset
    • @m - the rendered message (Note: do not add format specifiers like :lj or you'll lose theme color rendering. These format specifiers are not supported as they've become the default and only option - see the discussion here
    • @mt - the raw message template
    • @l - the event's level, as a LogEventLevel
    • @x - the exception associated with the event, if any, as an Exception
    • @p - a dictionary containing all first-class properties; this supports properties with non-identifier names, for example @p['snake-case-name']
    • @i - event id; a 32-bit numeric hash of the event's message template
    • @r - renderings; if any tokens in the message template include .NET-specific formatting, an array of rendered values for each such token
    • @tr - trace id; The id of the trace that was active when the event was created, if any
    • @sp - span id; The id of the span that was active when the event was created, if any

    For example the following filter:

    "Filter": [
      {
        "Name": "ByExcluding",
        "Args": {
          "expression": "Contains(@mt, 'Batch acquisition of')"
        }
      }
    ],
    

    will filter the second logging call:

    _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
    _logger.LogInformation("Batch acquisition of: {time}", DateTimeOffset.Now);
    

    As the "Contains(@m, 'Batch acquisition of')" too.

    Full repro project @github