Search code examples
.netsentry.net-core-service-worker

Setup Sentry with .Net Worker Service


I've seen how to integrate Sentry with a .Net web app and with a console app. But for a service worker, where the bootstrapping of the app is done with

IHost host = Host.CreateDefaultBuilder(args)
  .UseWindowsService(...)
  .ConfigureServices(...)

instead of

IHost host = Host.CreateHostBuilder(args)
  .ConfigureWebHostDefaults(webBuilder=>
  {
    webBuilder.UseSentry();
    webBuilder.UseStartup<Startup>();
  });

I have tried using the Sentry.AspNetCore package and the Sentry package, and boostrapping Sentry with different methods, including .ConfigureWebHostDefaults but that leads to the service not working (I get this error: No application configured. Please specify an application via IWebHostBuilder.UseStartup, IWebHostBuilder.Configure, or specifying the startup assembly via StartupAssemblyKey in the web host configuration.)

I've seen also this question but the answer given is not a service worker anymore.

So the question is: how can I use Sentry in a Service Worker app (that will become a Windows Service) ?

Thanks.


Solution

  • You can use the Sentry.Extensions.Logging NuGet package, and then add Sentry through the logging integration:

    IHost host = Host.CreateDefaultBuilder(args)
    // ...
    
    .ConfigureLogging(loggingBuilder) =>
    {
        loggingBuilder.AddSentry();
    })
    
    // ...
    

    There's a sample application in the sentry-dotnet repository here that shows that, along with some additional things - though it was made some time ago and needs to be updated to simplify.

    From the comment on your question, it looks like you already found this yourself. Yes, it's the correct way to do this for the time being. Eventually we will have a simpler API that uses IHostBuilder directly (see issue #190).

    You also asked:

    ... just wondering if that's equivalent as the asp.net helper functions?

    The ASP.NET Core integration starts with that, and then adds things that are specific for ASP.NET Core, such as a middleware component that automatically instruments each HTTP request/response. Such additions wouldn't be applicable in the service you described, but you could add custom instrumentation for your own features if you wanted to.