Serilog 未记录到与错误不同的哨兵级别

Serilog not logging to Sentry levels different than Error

我想使用 serilog 登录到 sentry.io Information 日志级别。

appsettings.json 我做了这个配置:

"Sentry": {
    "Dsn": "url",
    "MaxRequestBodySize": "Always",
    "SendDefaultPii": true,
    "IncludeActivityData": true,
    "AttachStackTrace": true,
    "Debug": true,
    "DiagnosticLevel": "Info"
  },
  "Serilog": {
    "Using": [
      "Serilog.Sinks.Console"
    ],
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "System": "Error",
        "Microsoft.EntityFrameworkCore.Database.Command": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "theme": "Serilog.Sinks.SystemConsole.Themes.SystemConsoleTheme::Literate, Serilog.Sinks.Console",
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Properties} {SourceContext} [{Level}] {Message:lj}{NewLine}{Exception}"
        }
      },
      {
        "Name": "Sentry",
        "Args": {
          "theme": "Serilog.Sinks.SystemConsole.Themes.SystemConsoleTheme::Literate, Serilog.Sinks.Console",
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Properties} {SourceContext} [{Level}] {Message:lj}{NewLine}{Exception}"
        }
      }
    ],
    "Enrich": [
      "FromLogContext",
      "WithMachineName",
      "WithThreadId",
      "WithHtpContextData",
      "WithExceptionDetails"
    ]
  }

我在 Program.cs class:

中注册了 serilog 和 sentry
public static IHostBuilder CreateHostBuilder(string[] args) =>                                                           
    Host.CreateDefaultBuilder(args)                                                                                      
        .ConfigureWebHostDefaults(webBuilder =>                                                                          
        {                                                                                                                
            webBuilder.UseStartup<Startup>();                                                                            
            webBuilder.UseSentry();                                                                                      
        })                                                                                                               
        .UseSerilog((hostingContext, loggerConfig) => loggerConfig.ReadFrom.Configuration(hostingContext.Configuration));

在我的 class 中,我编写了这样的代码:

using System.Threading.Tasks;
using Quartz;
using Serilog;

//

private readonly ILogger _logger;

public QueuedJob(ILogger logger)
{
    _logger = logger;
}

public Task Execute(IJobExecutionContext context)
{
    _logger.Information("Hello World!");
            
    return Task.CompletedTask;
}

为什么在 sentry.io 门户中的此配置中我只能看到我记录为 Error 级别的日志?为什么我无法登录 sentry.io Information 级别?所有级别的日志都打印到我的控制台,但只有 Errors 打印到控制台和 sentry.io

默认情况下,Sentry Serilog 集成仅发送日志级别 Error 或更高级别的事件。

对于 Info 日志,SDK 保留一个环形缓冲区,因此当发生错误时,所有相关日志都包含在该事件中。

虽然可以配置,但您可以发送所有内容(例如 Debug 或更高版本):https://docs.sentry.io/platforms/dotnet/guides/serilog/#configuration

事实上,我在 NuGet Trends 上使用了这个确切的设置来捕获任何 Warning 或更高版本的事件,并将任何 Debug 或更高版本的内容作为面包屑:

配置如下:

https://github.com/dotnet/nuget-trends/blob/dac67d1bd4707a94063b843571127eb055a4cc4f/src/NuGetTrends.Scheduler/appsettings.Production.json#L33-L34