c# - appsettings.json 中的 Sentry 配置与 .Net Core 3 控制台应用程序中的 Serilog

标签 c# .net-core sentry

我使用以下代码来配置 SentrySerilog在我的 .Net Core 控制台应用程序中:

return new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.Sentry(o =>
            {
                o.MinimumBreadcrumbLevel = LogEventLevel.Error;
                o.MinimumEventLevel = LogEventLevel.Error;
                o.Dsn = new Dsn(configuration.GetValue<string>("Sentry:Dsn"));
                o.Debug = false;
                o.AttachStacktrace = true;
                o.SendDefaultPii = true;
            })
            .CreateLogger();

现在我想将所有这些配置移至 appsettings.json文件,因为我需要在开发环境和生产环境中有不同的配置:

示例:

"Sentry": {
    "Dsn": "<dsn>",
    "IncludeRequestPayload": true,
    "IncludeActivityData": true,
    "Debug": false,
    "DiagnosticsLevel": "Debug",
    "MinimumBreadcrumbLevel": "Debug",
    "MinimumEventLevel": "Warning"
  },

是否可以从那里读取整个配置?

更新

我在控制台应用程序中尝试以下配置:

static void Main(string[] args)
    {
        using (SentrySdk.Init("<dsn>"))
        {
            // my code
        }

...

serviceCollection.AddSingleton<ILogger>(s =>
        {
            return new LoggerConfiguration()
            .ReadFrom.Configuration(configuration)
            .CreateLogger();
        });

应用程序设置:

"Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.Sentry" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning",
        "Sentry": "Information"
      }
    },
    "Enrich": [ "FromLogContext" ],
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "Sentry",
        "Args": {
          "Dsn": "<dsn>",
          "MinimumBreadcrumbLevel": "Info",
          "MinimumEventLevel": "Info",
          "AttachStackTrace": true, 
          "Debug": true,
          "DiagnosticsLevel": "Info"
        }
      }
    ]

但是SentrySdk.Init("<dsn>")需要 DSN,但它不会从应用程序设置中读取它!

是这样吗?

最佳答案

Serilog 不知道 ASP.NET Core 配置。至少以某种方式将配置对象的特定部分绑定(bind)到其扩展。

如果您使用 ASP.NET Core,我假设您也在使用 Sentry.AspNetCore。建议你这样做。在这种情况下,您也需要将配置传递给它。

NuGetTrends这是一个开源项目,使用 Serilog 和 ASP.NET Core,同时使用 appsettings.json 文件进行配置。

Here's the Program.cs setting up Sentry, Serilog and ASP.NET Core.

请注意,您提到的配置(以 Sentry 作为键)将由 Sentry.AspNetCore 获取,但不会由 Sentry.Serilog 获取code>(与 ASP.NET Core 解耦)。

您可以配置后者,它是一个普通的 Serilog 接收器,例如:

  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning",
        "Sentry": "Information"
      }
    },
    "WriteTo": [
       {
        "Name": "Sentry",
        "Args": {
          "MinimumBreadcrumbLevel": "Debug",
          "MinimumEventLevel": "Warning"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName" ]
  }

我建议像您一样保留 Sentry 键上的 DSN 和其他设置,这使得 ASP.NET Core 初始化 SDK,它还将利用Serilog 集成。

关于c# - appsettings.json 中的 Sentry 配置与 .Net Core 3 控制台应用程序中的 Serilog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60057527/

相关文章:

angular - 错误 : File lib. dom.d.ts 没有源文件

c# - 在 C# 中比较数组的最简单方法

c# - 使用 EF Core 的 Visual Studio 2017 在迁移之前/之后更改 mdf 文件的本地数据库默认位置

docker - 在Docker容器中未创建环境变量

c# - 如何在 dotnet core 中获取当前连接的 WiFi SSID 和信号强度?

ruby-on-rails - Rspec 测试 Sentry 的 Raven capture_exception

c# - WCF 服务的线程问题

c# - 如何并行运行 NUnit(Selenium Grid)测试?

.net-core - 如何将 MediatR 与我的业务层分离

javascript - 在 Next.js 项目中集成 Sentry