Serilog : how to NOT log some requests (or to log with different levels)

有些话、适合烂在心里 提交于 2021-02-10 13:03:46

问题


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

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 ?

Program.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 file :

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

回答1:


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

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();

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.



来源:https://stackoverflow.com/questions/65343466/serilog-how-to-not-log-some-requests-or-to-log-with-different-levels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!