简体   繁体   English

如何将 Sentry 与 .NET 6.0 Worker Service 集成?

[英]How to integrate Sentry with .NET 6.0 Worker Service?

I integrated Sentry with .NET Core 6.0 Worker Service this way:我以这种方式将Sentry与 .NET Core 6.0 Worker Service集成:

NuGet: Sentry 3.17.1 NuGet:哨兵 3.17.1

// Program.cs:

using Sentry;

var sentryDsn = Environment.GetEnvironmentVariable("SENTRY_DSN");
using (SentrySdk.Init(o =>
{
    o.Dsn = sentryDsn;
    o.Debug = true;
    o.TracesSampleRate = 1.0;
}))
{
    IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddHostedService<Worker>();
    })
    .Build();

    await host.RunAsync();
}
// Worker.cs:

namespace demo_heroku_sentry_worker;
using Sentry;

public class Worker : BackgroundService
{
    private readonly ILogger<Worker> _logger;

    public Worker(ILogger<Worker> logger)
    {
        _logger = logger;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

            try
            {
                throw new ApplicationException("Exception inside of worker service");
            }
            catch (Exception e)
            {
                SentrySdk.CaptureException(e);
            }

            await Task.Delay(10000, stoppingToken);
        }
    }
}

This is working in some way because I see the manually captured error on my Sentry Dashboard.这在某种程度上是有效的,因为我在我的哨兵仪表板上看到了手动捕获的错误。 However I'm concerned about these warning messages I receive on the Application Output:但是,我担心我在应用程序输出中收到的这些警告消息:

      Worker running at: 05/11/2022 15:51:06 +02:00
  Debug: Failed to report an error on a session because there is none active.
   Info: Capturing event.
  Debug: Running processor on exception: Exception inside of worker service
  Debug: Creating SentryStackTrace. isCurrentStackTrace: False.
  Debug: Running main event processor on: Event abb5b3e2ee3a4dbd***********
   Info: Envelope queued up: 'abb5b3e2ee3a4dbda50ef***********'
  Debug: Envelope abb5b3e2ee3a4dbda50e*********** handed off to transport. #1 in queue.
  Debug: Envelope 'abb5b3e2ee3a4dbda50efe7***********' sent successfully. Payload:

Is there something I am missing?有什么我想念的吗?

It seems that as per the documentation the correct way to integrate Sentry with .NET (Core) 6.0 is the following: (changes indicated <-- )似乎根据文档,将 Sentry 与 .NET (Core) 6.0 集成的正确方法如下:(更改指示<--

// Program.cs

var builder = WebApplication.CreateBuilder(args);

var sentryDsn = Environment.GetEnvironmentVariable("SENTRY_DSN");  // <--
builder.WebHost.UseSentry(sentryDsn);                              // <--

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseSentryTracing();                                            // <--

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

It does catch unhandled API call errors and it does not print any warnings on the output console.它确实会捕获未处理的 API 调用错误,并且不会在输出控制台上打印任何警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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