1

I'm using serilog and try to filter out HTTP Methods that are not POST, GET, PUT, and DELETE. for example filter out OPTIONS Method.

I also use UseSerilogRequestLogging and Serilog.Expressions.

Everything work fine, Serilog is great. But the filter does't work. Any idea why?

Here is my code:

Startup.cs:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory 
    loggerFactory)
{
    ...
    app.UseSerilogRequestLogging(opt => opt.EnrichDiagnosticContext = 
    LogHelper.EnrichFromRequest);
    ...
}

LogHelper.cs:

public static class LogHelper
    {
        public static async void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext)
        {
            var request = httpContext.Request;
            diagnosticContext.Set("Protocol", request.Protocol);
        }
    }

and appsettings.json:

  "Serilog": {
    "Using": [ "Serilog.Expressions" ],
    "Enrich": [ "FromLogContext" ],
    "Filter": [
      {
        "Name": "ByExcluding",
        "Args": {
          "expression": "@Properties['Protocol'] like '%OPTIONS%'"
        }
      }
    ]
  },
Citizen-Dror
  • 843
  • 9
  • 17

1 Answers1

0

A shot in the dark, but have you tried either of these formats instead?

"Args": {
      "expression": "Protocol like '%OPTIONS%'"
}

"Args": {
      "expression": "Contains(Protocol, 'OPTIONS') ci"
}
hobwell
  • 538
  • 1
  • 8
  • 26