Serilog LoggerConfiguration 相当于具有日志级别的 AddFilter

Serilog LoggerConfiguration equivalent of AddFilter with log level

我现在的代码是这样的:

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) {
      return WebHost.CreateDefaultBuilder(args)
        .ConfigureLogging((hostingContext, logging) => {
          logging.AddFilter("Microsoft.AspNetCore.SignalR", LogLevel.Debug);
          logging.AddFilter("Microsoft.AspNetCore.Http.Connections", LogLevel.Debug);
        })
        .UseSerilog(Log.Logger)

我现在收到描述错误:

warning CS0618: 'SerilogWebHostBuilderExtensions.UseSerilog(IWebHostBuilder, ILogger, bool, LoggerProviderCollection)' is obsolete: 'Prefer UseSerilog() on IHostBuilder'

我的理解是我现在需要像这样创建记录器:

    public static void Main(string[] args) {
      Log.Logger = new LoggerConfiguration()
        // set filters ???
        .CreateLogger();

然后 UseSerilog()CreateWebHostBuilder

但是如何在 LoggerConfiguration 中设置等效过滤器?我好像找不到相关的例子。

该错误告诉您从 Microsoft.AspNetCore.Hosting.IWebHostBuilder 移动到 Microsoft.Extensions.Hosting.IHostBuilder

IWebHostBuilder 适用于 .NET Core 2,请参阅 documentation

您的代码如下所示。
(也重命名了那个方法。)

public static IHostBuilder CreateHostBuilder(string[] args)
{
    return Host.CreateDefaultBuilder(args)
      .ConfigureLogging((hostingContext, logging) => {
          logging.AddFilter("Microsoft.AspNetCore.SignalR", LogLevel.Debug);
          logging.AddFilter("Microsoft.AspNetCore.Http.Connections", LogLevel.Debug);
      })
      .UseSerilog(Log.Logger);
}

如果无法迁移,则必须选择旧版本 Serilog