Serilog:如何不记录某些请求(或以不同级别记录) [英] Serilog : how to NOT log some requests (or to log with different levels)

查看:94
本文介绍了Serilog:如何不记录某些请求(或以不同级别记录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我启用 Serilog 来记录对我的服务器发出的所有 HTTP 请求(请参阅下面的 Program.cs 和 logging.json).

At the moment, I enable Serilog to log all the HTTP requests made to my server (see Program.cs and logging.json below).

但是,Openshift 正在调用/ready 和/live,我不想记录这些请求

However, Openshift is calling /ready and /live and I do not want to log these requests

{"@t":"2020-12-17T15:02:08.7321442Z","@m":"HTTP \"GET\" \"/ready\" responded 200 in 41.3777 ms","@i":"62d0885c","RequestMethod":"GET","RequestPath":"/ready","StatusCode":200,"Elapsed":41.3777,"SourceContext":"Serilog.AspNetCore.RequestLoggingMiddleware","RequestId":"0HM52JISL6NBA:00000001","SpanId":"|d7e25f1-47c5ac680b1d5fd1.","TraceId":"d7e25f1-47c5ac680b1d5fd1","ParentId":"","ConnectionId":"0HM52JISL6NBA"}

问题是我仍然想记录其他请求...

The problem is that I still want to log OTHER requests...

有人可以向我解释我可以在哪里钩住这个逻辑吗?

Can someone explain to me where I can hook this logic ?

程序.cs

class Program
{
    static async Task Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(provider: ConfigMapFileProvider.FromRelativePath("conf"), path: "logging.json", optional: false, reloadOnChange: true)
                .Build()
            )
            .Enrich.FromLogContext()
            .WriteTo.Console(new RenderedCompactJsonFormatter())
            .CreateLogger();

        await Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddJsonFile("conf/network.json", optional: false, reloadOnChange: false);
                config.AddJsonFile("conf/messagebus.json", optional: false, reloadOnChange: false);
                config.AddJsonFile("conf/auth.json", optional: false, reloadOnChange: false);
                config.AddJsonFile("conf/appsettings.json", optional: false, reloadOnChange: false);
            })
            .ConfigureWebHostDefaults(options =>
            {
                options.UseSerilog();
                options.UseKestrel();
                options.UseStartup<Startup>();
            })
            .RunConsoleAsync();
    }
}

logging.json 文件:

logging.json file :

{
  //
  // LOGGING
  //
  "Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Information",
        "Microsoft.AspNetCore": "Warning",
        "Microsoft.EntityFrameworkCore": "Warning",
        "Grpc.AspNetCore": "Warning",
        "ProtoBuf.Grpc": "Warning"
      }
    }
  }
}

推荐答案

Serilog 支持 过滤器,您可以根据每个日志事件的属性有选择地包括或排除记录的事件.

Serilog supports Filters that you can apply to selectively include or exclude events from being logged based on the properties of each log event.

在您的情况下,使用 .Filter.ByExclusion 删除 RequestPath 的特定值上的日志条目可以解决问题:

In your case, a .Filter.ByExcluding to remove log entries on specific values of RequestPath would do the trick:

using Serilog;
using Serilog.Filters;
// ...

Log.Logger = new LoggerConfiguration()
    .Filter.ByExcluding(
        Matching.WithProperty<string>("RequestPath", v =>
            "/ready".Equals(v, StringComparison.OrdinalIgnoreCase) ||
            "/live".Equals(v, StringComparison.OrdinalIgnoreCase)))
    .WriteTo.Console()
    .CreateLogger();

当然,您的日志事件还有其他有趣的属性,例如 RequestMethodSourceContext,例如,您还可以使用它们来使过滤条件更加具体,如果你愿意.

Of course, your log events have other interesting properties such as RequestMethod, and SourceContext, for example, that you can also use to make the filter criteria more specific, if you want.

这篇关于Serilog:如何不记录某些请求(或以不同级别记录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆