Skip to content

Improve guidance for .NET Worker Services #2274

Not planned

Description

@mattjohnsonpint

Problem Statement

We should provide documentation and samples for integrating Sentry with .NET Worker Services.

Note, we already have the Generic Host sample, but this is slightly different.

Solution Brainstorm

  • Build a sample starting from dotnet new worker
  • Add Sentry.Extensions.Logging
  • Clear the scope before the first execution (requires Add Scope.Clear and Scope.ClearBreadcrumbs #2272)
  • Push and dispose a new scope for each inner loop of the worker
  • Enable tracing, and start/finish a new transaction for each inner loop of the worker
  • Add a new guide to the Sentry .NET docs

Activity

  1. mattjohnsonpint commented on Apr 14, 2023

    @mattjohnsonpint
    ContributorAuthor

    Also, explain clearly that global mode should be false for background workers. Update docs for IsGlobalMode.

    See https://twitter.com/vekzdran/status/1646886760096362497

  2. mattjohnsonpint commented on Apr 14, 2023

    @mattjohnsonpint
    ContributorAuthor

    POC:

    public class Worker : BackgroundService
    {
        private readonly IHub _hub;
    
        public Worker(IHub hub)
        {
            _hub = hub;
        }
    
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                using var _ = _hub.PushScope();
                var transaction = _hub.StartTransaction($"{nameof(Worker)}.{nameof(DoWorkAsync)}", "function");
                try
                {
                    _hub.ConfigureScope(scope =>
                    {
                        scope.Clear();
                        scope.Transaction = transaction;
                    });
    
                    await DoWorkAsync(stoppingToken);
                }
                catch(Exception exception)
                {
                    transaction.Finish(exception);
                }
                finally
                {
                    transaction.Finish();
                }
            }
        }
    }
  3. mattjohnsonpint commented on Apr 18, 2023

    @mattjohnsonpint
    ContributorAuthor

    Updated POC. Requires vNext (3.31.0) for #2303 & #2309.

    public class Worker : BackgroundService
    {
        private readonly IHub _hub;
    
        public Worker(IHub hub)
        {
            _hub = hub;
        }
    
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                await _hub.WithScopeAsync(async scope =>
                {
                    scope.Clear();
    
                    var transaction = _hub.StartTransaction($"{nameof(Worker)}.{nameof(DoWorkAsync)}", "function");
                    scope.Transaction = transaction;
    
                    try
                    {
                        await DoWorkAsync(stoppingToken);
                    }
                    catch (Exception exception)
                    {
                        transaction.Finish(exception);
                    }
                    finally
                    {
                        transaction.Finish();
                    }
                });
            }
        }
    }
  4. removed their assignment
    on Jun 20, 2023
  5. Kampfmoehre commented on Jan 24, 2024

    @Kampfmoehre

    @mattjohnsonpint Just a quick follow up question, how would I retrieve a transaction like the one started in your sample? With SentrySdk.GetSpan()?

    ISpan? span = SentrySdk.GetSpan();
    if (span is ITransaction transaction)
    {
      transaction.StartChild("Something");
    }
  6. 20 remaining items

  7. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Aug 20, 2024
  8. added and removed
    Feature
    on May 7, 2025
  9. added
    Feature
    on Aug 6, 2025
  10. removed theissue type on Aug 6, 2025
  11. added
    .NET
    on Mar 24, 2026
  12. jamescrosswell commented on Nov 26, 2026

    @jamescrosswell
    Collaborator

    Closing. The Generic Host guide now covers worker services, with a BackgroundService example and guidance to start a transaction per unit of work, and samples/Sentry.Samples.GenericHost shows the setup. If something specific is still missing, please open a docs issue for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETDocsFeatureSamples

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions