---
title: "Hangfire"
description: "Learn more about how to monitor your Hangfire jobs."
url: https://docs.sentry.io/platforms/dotnet/guides/extensions-logging/crons/hangfire/
---

# Hangfire for Microsoft.Extensions.Logging

The .NET SDK provides an integration with [Hangfire](https://www.hangfire.io/) to monitor your jobs by automatically [creating check-ins for them](https://docs.sentry.io/product/monitors-and-alerts/monitors/crons/job-monitoring.md). The SDK relies on job filters that are set up when you call `UseSentry`. For example:

```csharp
using Hangfire;
using Sentry.Hangfire;

GlobalConfiguration.Configuration.UseSentry();
```

Alternatively, if you're using the builder pattern, your code may look like this:

```csharp
using Hangfire;
using Sentry.Hangfire;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHangfire(configuration => configuration
    .UseSentry() // <- Add Sentry to automatically send check-ins
);
```

For each monitored job, 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.

## [Monitor All Recurring Jobs](https://docs.sentry.io/platforms/dotnet/guides/extensions-logging/crons/hangfire.md#monitor-all-recurring-jobs)

We recommend monitoring every recurring job. From SDK version 6.13.0, you can do this without adding the `SentryMonitorSlug` attribute to each one. Turn on `MonitorAllRecurringJobs`, and add any recurring jobs you don't want to monitor to `ExcludedRecurringJobs`, by their exact ID (case-sensitive) or with a regular expression:

```csharp
using System.Text.RegularExpressions;
using Hangfire;
using Sentry.Hangfire;

GlobalConfiguration.Configuration.UseSentry(options =>
{
    options.MonitorAllRecurringJobs = true;
    options.ExcludedRecurringJobs.Add("cache-warmup");
    options.ExcludedRecurringJobs.Add(new Regex("^cleanup-", RegexOptions.IgnoreCase));
});
```

The monitor slug is the recurring job ID, normalized the same way Sentry normalizes monitor slugs: lowercased, accents removed, spaces and dashes replaced by a single `-`, other characters except `_` dropped, and cut to 50 characters. For example, the recurring job `Reports: Nightly Run` reports to the monitor `reports-nightly-run`, and `cache_warmup` to `cache_warmup`. Jobs with the `SentryMonitorSlug` attribute keep their slug and are monitored even if their ID is excluded.

The schedule is sent as described in [Send the Recurring Job Schedule](https://docs.sentry.io/platforms/dotnet/guides/extensions-logging/crons/hangfire.md#send-the-recurring-job-schedule). Recurring jobs whose schedule Sentry doesn't support aren't monitored. To monitor one anyway, add the `SentryMonitorSlug` attribute and set up its monitor in Sentry. Recurring jobs that end up with the same monitor slug as another job with a different schedule aren't monitored either. Change one of their IDs so each gets its own slug. Jobs that aren't recurring, such as `BackgroundJob.Enqueue`, still need the attribute.

## [Monitor Specific Jobs](https://docs.sentry.io/platforms/dotnet/guides/extensions-logging/crons/hangfire.md#monitor-specific-jobs)

To monitor specific jobs, including jobs that aren't recurring, provide the job with a monitor slug by setting the `SentryMonitorSlug` attribute on it. For example, the following snippet shows how to set up a fire-and-forget job that Sentry will monitor:

```csharp
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
    }
}
```

With `MonitorAllRecurringJobs` on, a recurring job with the attribute still sends one check-in per run, using the attribute's slug.

## [Send the Recurring Job Schedule](https://docs.sentry.io/platforms/dotnet/guides/extensions-logging/crons/hangfire.md#send-the-recurring-job-schedule)

Sentry only creates a monitor from a check-in that includes a monitor config. From SDK version 6.13.0, the SDK sends the schedule of your recurring jobs with their check-ins by default.

The job still needs the `SentryMonitorSlug` attribute, unless you [monitor all recurring jobs](https://docs.sentry.io/platforms/dotnet/guides/extensions-logging/crons/hangfire.md#monitor-all-recurring-jobs).

```csharp
using Hangfire;
using Sentry.Hangfire;

GlobalConfiguration.Configuration.UseSentry();
RecurringJob.AddOrUpdate<PricingUpdateWorker>("update-pricing", job => job.Execute(), Cron.Hourly());

public class PricingUpdateWorker
{
    [SentryMonitorSlug("update-pricing")]
    public void Execute()
    {
        // Do your work to update the price
    }
}
```

The SDK sends the job's cron expression and time zone each time the job runs. Sentry creates the monitor on the first check-in and sets its schedule and time zone from the job on every check-in after that, so changes you make to those two settings in Sentry are overwritten. Other monitor settings are kept.

If Sentry doesn't support the job's cron expression or time zone, a job with the `SentryMonitorSlug` attribute sends its check-ins without a schedule, and [monitoring all recurring jobs](https://docs.sentry.io/platforms/dotnet/guides/extensions-logging/crons/hangfire.md#monitor-all-recurring-jobs) skips the job. Unsupported schedules include crons that run every few seconds, crons that restrict both the day of the month and the day of the week, and Windows time zone IDs on .NET Framework.

If several recurring jobs use the same monitor slug with different cron expressions or time zones, for example one recurring job per region that runs the same method, none of them sends a schedule. To monitor jobs that run the same method on different schedules, remove the attribute and turn on [`MonitorAllRecurringJobs`](https://docs.sentry.io/platforms/dotnet/guides/extensions-logging/crons/hangfire.md#monitor-all-recurring-jobs), so each recurring job gets its own slug from its ID.

When you upgrade, existing monitors for recurring jobs get their schedule and time zone from the job. To keep managing the schedule in Sentry instead, turn off `SendMonitorConfig`:

```csharp
GlobalConfiguration.Configuration.UseSentry(options => options.SendMonitorConfig = false);
```

If you delete a monitor while its job is still monitored, the next run creates it again. To stop monitoring a job, disable the monitor in Sentry, or remove the attribute and, if you [monitor all recurring jobs](https://docs.sentry.io/platforms/dotnet/guides/extensions-logging/crons/hangfire.md#monitor-all-recurring-jobs), add the job to `ExcludedRecurringJobs`.
