Serilog builder.Host 和 AddFilter

Serilog builder.Host and AddFilter

我有一个使用简洁的 .NET 6/7 serilog 初始化的网络应用程序:

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseSerilog((ctx, lc) => {
  lc.WriteTo.File(
    logDir + "\mylog.log"

  ).Filter.ByExcluding(le => {
    var s = le.RenderMessage();
    return s.StartsWith("Executing endpoint")
      || s.StartsWith("Executed endpoint")
      || s.StartsWith("User logged in.");
  });
});

我现在想过滤一些消息:

AddFilter("Microsoft.AspNetCore.SignalR", LogLevel.Debug);
AddFilter("Microsoft.AspNetCore.Http.Connections", LogLevel.Debug);

但是我不确定如何将这些 AddFilter 语句插入到上面的构建器中

有什么帮助吗?

如果要省略Microsoft.Namespace日志。 JSON 配置如下所示:

您需要安装Serilog.Filters.Expressions nuget 包

"Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning",
        "Microsoft.EntityFrameworkCore": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore')"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "C:\LOGS\TestService_EF_.json",
                  "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
                  "rollingInterval": "Day",
                  "retainedFileCountLimit": 7
                }
              }
            ]
          }
        }
      }
    ]
  }

对于基于代码的方法,请使用 MinimumLevel.Override

builder.UseSerilog(
    (ctx, lc) => lc
        .WriteTo.File(logDir + "\mylog.log")
        .Filter.ByExcluding(le => {
            var s = le.RenderMessage();
            return s.StartsWith("Executing endpoint")
                || s.StartsWith("Executed endpoint")
                || s.StartsWith("User logged in.");
        })
        .MinimumLevel.Override("Microsoft.AspNetCore.SignalR", Serilog.Events.LogEventLevel.Debug)
        .MinimumLevel.Override("Microsoft.AspNetCore.Http.Connections", Serilog.Events.LogEventLevel.Debug)
    );

对于配置方法,使用ReadFrom.Configuration

builder.UseSerilog(
    (ctx, lc) => lc
        .WriteTo.File(logDir + "\mylog.log")
        .Filter.ByExcluding(le => {
            var s = le.RenderMessage();
            return s.StartsWith("Executing endpoint")
                || s.StartsWith("Executed endpoint")
                || s.StartsWith("User logged in.");
        })
        .ReadFrom.Configuration(ctx.Configuration)
    );

您的 appsettings.json 文件应如下所示。
日志级别可能不同。

{
    "Logging": {
        "LogLevel": {
            "Default": "Warning",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Warning"
        }
    },
    "Serilog": {
        "MinimumLevel": {
            "Default": "Warning",
            "Override": {
                "Microsoft.AspNetCore.SignalR": "Debug",
                "Microsoft.AspNetCore.Http.Connections": "Debug"
            }
        }
    }
}