简体   繁体   English

Serilog .NET 核心:过滤请求日志并将它们发送到单独的接收器

[英]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();我似乎找不到过滤请求登录跟踪的方法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.这是我尝试使用的 API,尽管我没有找到任何表明该事件是请求日志记录事件的属性。

.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:来自UseSerilogRequestLogging请求日志都具有相同的模板,因此您可以使用:

  .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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET Serilog PostgresSql 接收器中的核心自定义列 - .NET Core custom columns in Serilog PostgresSql sink .Net 核心日志记录到 PostgreSQL 与 Serilog 不工作 - .Net Core Logging to PostgreSQL with Serilog Not Working Serilog文件接收器未记录到文件 - Serilog File sink not logging to file 使用控制台作为 readkey 输入和作为 serilog 日志记录的接收器 - Using the console as readkey input AND as sink for serilog logging 在 ASP.NET 核心 (.NET5) 中,如何将每个请求的日志写入单独的文件? 使用 Serilog 或其他 - in ASP.NET core (.NET5) how can i write logs for each request on separate files? Using Serilog or other 在 asp.net 内核中使用 serilog 维护单独的 scope 上下文 - Maintaining separate scope context with serilog in asp.net core 配置 .net 核心日志记录以记录请求和响应 - Configure .net core logging to log request and response Serilog 使用 Microsoft ILogger - 从 .NET Core 中的一个代码记录到不同的位置 - Serilog using Microsoft ILogger - logging to different places from one code in .NET Core 在 .NET 中使用 Serilog 更高效的日志记录是什么? - What is more efficient logging in Serilog in .NET? Serilog 日志库可以在 .net WPF 应用程序中使用吗? - Will the Serilog logging library work in a .net WPF application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM