5

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"
      }
    }
  }
}
Christophe Blin
  • 1,687
  • 1
  • 22
  • 40

1 Answers1

7

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.

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
  • To add to this answer, some use plain text file logging which usually does not reveal all of the log event properties, to reveal all of them you can turn on json logging which creates a structure complex object which captures an entire snapshot and logs it to disk into a .json file. You have to use the built in `CompactJsonFormatter` or the `JsonFormatter` for this to work. – IbrarMumtaz Aug 31 '23 at 12:16