Serilog .NET core: Filter Request logging and send them to a separate sink

Viewed 886

I want to keep my requests logs on a separate log file. I cannot seem to find a way to filter Requests login traces app.UseSerilogRequestLogging();.

Is there any way of achieving this? I do not find any documentation.

This is the API I am trying to use, although I do not find any property that indicates the event is a request logging event.

.WriteTo.Logger(config => config
        .MinimumLevel.Verbose()
        .Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Debug)
1 Answers

Request logs from UseSerilogRequestLogging all have the same template, so you can use:

  .Filter.ByIncludingOnly(e => e.MessageTemplate.Text == 
    "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms")

(Assuming you're using the default template.)

Alternatively, since they all come from the same class:

var requestLogs = Matching.FromSource("Serilog.AspNetCore.RequestLoggingMiddleware");

// ... then:
  .Filter.ByIncludingOnly(requestLogs)
Related