c# - Serilog 未记录到与错误不同的 Sentry 级别

标签 c# asp.net-core serilog sentry

我想使用 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"
    ]
  }

我将 serilog 和 Sentry 注册到我的 Program.cs 类中:

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

在我的类里面,我编写了这样的代码:

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 会保留一个环形缓冲区,因此当发生错误时,所有相关日志都会包含在该事件中。

Sentry Breadcrumbs

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

事实上,我在 NuGet Trends 上使用了这个确切的设置。捕获任何警告或更高级别的事件,并将任何调试或更高级别的内容作为面包屑:

这是配置:

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

关于c# - Serilog 未记录到与错误不同的 Sentry 级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66655557/

相关文章:

c# - 仅在ISE中运行的Powershell脚本

c# - mailto 的更好替代方案

c# - 远程托管服务器上 Web 服务的间歇性问题

c# - Serilog 只记录数组的第一个元素

c# - XUnit 和 ITestOutputHelper 实例化

c# - 国际化 mvc3 - 哪个是最好的方法? session , cookies ?

visual-studio - Visual Studio ASP.NET Core 调试 IIS Express - 无法访问站点

asp.net-core - 如何在 .NET Core 中选择 IOptions 部分?

c# - 在 ASP.NET vNext 中,为什么不即时重新编译代码?

c# - Serilog - 单独的信息文件,使用 appsetting.json 的异常(exception)