简体   繁体   中英

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

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM