Hangfire
The .NET SDK provides an integration with Hangfire to monitor your jobs by automatically creating check-ins for them. The SDK relies on job filters that are set up when you call UseSentry
. For example:
using Hangfire;
using Sentry.Hangfire;
GlobalConfiguration.Configuration.UseSentry();
Alternatively, if you're using the builder pattern, your code may look like this:
using Hangfire;
using Sentry.Hangfire;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHangfire(configuration => configuration
.UseSentry() // <- Add Sentry to automatically send check-ins
);
To let the SDK know which jobs to monitor, you need to provide the job with a monitor slug. This can be done by setting the SentryMonitorSlug
attribute on the job. For example, the following snippet shows how to set up a fire-and-forget job that Sentry will monitor:
using Hangfire;
using Sentry.Hangfire;
GlobalConfiguration.Configuration.UseSentry();
BackgroundJob.Enqueue<PricingUpdateWorker>(job => job.Execute());
public class PricingUpdateWorker
{
[SentryMonitorSlug("update-pricing")]
public void Execute()
{
// Do your work to update the price
}
}
The SDK will automatically capture a check-in with CheckInStatus.InProgress
when the job starts and update the check-in with CheckInStatus.OK
on success. In case of an exception during execution, the check-in will be updated as CheckInStatus.ERROR
instead.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").
- Package:
- nuget:Sentry.Serilog
- Version:
- 4.3.0
- Repository:
- https://github.com/getsentry/sentry-dotnet