asp.netblazor-server-sideserilogserilog-aspnetcoreserilog-filter

Serilog logging too much information


I have a Blazor Server app being hosted in a Azure App Service and want to customize the logging. I’m trying Serilog for the first time and after watching a couple videos I as able to get it to at least write out the console fairly easy. However, I get a whole bunch of extra .Net chatter that I would like to exclude. I’m assuming once I push the code out to Azure it would also log stuff going on with Azure itself.

Is it possible to exclude all of the extra logging? I found an article that talks about setting the Database.Command:None and Infrastructure:None. That seemed like it worked when I was using just the built in ILogger but doesn’t seem to effect Serilog.

Are there settings in Serilog with I can have it just log what I put in my apps call to _log.LogInformation or _log.LogError?

Nuget Package

Serilog.AspNetCore

Program.cs

builder.Logging.ClearProviders();
builder.Host.UseSerilog((context, config) => config.ReadFrom.Configuration(context.Configuration));

Appsettings

"Serilog": {
  "MinimumLevel": {
    "Default": "Information",
    "Override": {
      "Microsoft.AspNetCore": "Information"
    }
  },
  "WriteTo": [
    {
      "Name": "Console",
      "Args": {
        "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u4}: {Message:lj}{NewLine}{Exception}]"
      }
    }
  ],
  "Microsoft.EntityFrameworkCore.Database.Command": "None",
  "Microsoft.EntityFrameworkCore.Infrastructure": "None"
},

Currently what I see in the console. enter image description here


Solution

  • You'll have to configure both the general and Serilog related levels.

    To filter out the logging that does not originate from your own code, set it a to a high level like warning or error. You don't want to completely silence it, otherwise you'll miss any real errors.

    Configure a lower level -- e.g. information -- as a Serilog override, matching the namespace (or a part of it) of your application.

    As an example, in case your application would have namespaces Contoso.Application.Server and Contoso.Application.Client, then below Contoso registration would cover both and any informational and upwards log statements from code within that namespace would be included in the log output.

    {
      "Logging": {
        "LogLevel": {
          "Default": "Error"
        }
      },
      "Serilog": {
        "MinimumLevel": {
          "Default": "Error",
          "Override": {
            "Contoso": "Information"        
          }
        }
      }
    }