Search code examples
c#.net-coresentry

Sentry configuration in appsettings.json with Serilog in .Net Core 3 Console App


I'm using the following code to configure Sentry with Serilog in my .Net Core Console application:

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();

Now I would like to move all this configuration in the appsettings.json file because I need to have a different configuration in Develop environment and Production:

Example:

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

Is it possible to read the whole configuration from there?

update

I try the following configuration in my Console application:

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

...

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

appsettings:

"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"
        }
      }
    ]

But SentrySdk.Init("<dsn>") needs a DSN and it won't read it from appsettings!

Is this the way?


Solution

  • Serilog is unaware of the ASP.NET Core configuration. At least in a way that would bind specific sections of the configuration object to its extensions.

    If you're using ASP.NET Core, I assume you're also using Sentry.AspNetCore. It's advised you do. In which case you'd need to pass configuration to it too.

    NuGetTrends which is an open source project uses Serilog with ASP.NET Core while using the appsettings.json file to configure things.

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

    Note that the configuration as you mentioned (with Sentry as a key) will be picked up by Sentry.AspNetCore but not by Sentry.Serilog (which is decoupled from ASP.NET Core).

    You can configure the latter, which is a normal Serilog sink like:

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

    I suggest leaving the DSN and other settings as you did, on the Sentry key, which makes the ASP.NET Core initialize the SDK, it will also leverage the Serilog integration.