简体   繁体   中英

Serilog not logging to Sentry levels different than Error

I want log to sentry.io Information log level using serilog.

In appsettings.json I made this config:

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

I registered serilog and sentry into my Program.cs class:

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

In my class i make code like this:

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;
}

Why from this configuration in sentry.io portal i see only logs that i logged as Error level? Why i cant log into sentry.io Information level? All levels of logs are printed to my console but only Errors are printed into console and sentry.io

By default, the Sentry Serilog integration only sends events for log level Error or higher.

For Info logs, the SDK keeps a ring buffer so when an error happens all related logs are included with that event.

哨兵面包屑

This can be configured though, you can send everything ( Debug or higher for example): https://docs.sentry.io/platforms/dotnet/guides/serilog/#configuration

In fact, I use this exact setup on NuGet Trends to capture any Warning or higher as event, and include any Debug or higher as breadcrumb:

Here's the configuration:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM