question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serilog + WorkerService + IHostBuilder

See original GitHub issue

I’ve been searching for quite a while how to make Serilog and Sentry work together in a WorkerService environment (.net core > 3) I’ve come to the conclusion that this is not easily done / possible right now due to some limitation:

Known issue:

  • There is no IHostBuilder extension method for sentry

Unable to ‘hack’ together what I need by using internals / copy pasting

Work around

The current ‘work-around’ I have is using the Serilog sink and let it initialise the SDK, and add the following in ConfigureServices:

WARNING This is not production tested, just to make it work locally

services.TryAddTransient<IHub>((_) => HubAdapter.Instance);
services.TryAddTransient<ISentryClient>((_) => HubAdapter.Instance);
services.TryAddTransient<ISentryScopeManager>((_) => HubAdapter.Instance);

However this way you lose the extensibility of Sentry together with DI

There might be other ways, in which case feel free to add them 😃

Proposal

First an observation, IWebHostBuilder on a high level does 2 things (i know it’s a bit reductive):

  • (1) Integrates Sentry with the AspNetCore host (DI, Logging, HostLifetime, …)
  • (2) Decorates the request pipeline using a middleware

Proposed changes:

  • Add IHostBuilder extension method
    • UseSentryCore that handles (1)
  • Add IWebHostBuilder extension method
    • UseSentryWeb that handles (2)
  • Keep IWebHostBuilder extension method
    • UseSentry for backwards compatibility

Analysis

This is after a short, preliminary & quick swoop through the code base:

Split following classes into core and web (or add methods to support them)

I might be able to free some time for a PR once this proposal is finalised

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Vandersteencommented, May 25, 2021

Ok, thank you for the feedback. I’ll work on a first draft tomorrow

0reactions
mattjohnsonpintcommented, May 10, 2022

Blocked on #190. Keeping this open for followup after that is done.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NET Core Worker Services with Application Insights and Serilog
The .NET Core Worker service is yet another tool in the .NET toolbox. They are perfect for background processing like reading from a...
Read more >
Structured Logging in .NET Core Worker Service with Serilog
In this post, I'll show how to use Serilog in a .NET Worker Service to perform Structured Logging and persist log data to...
Read more >
Serilog file logging not written from Worker Service when ...
I am writing a worker service that needs to use Serilog and write to a log file. (.net core 6 and Windows service)....
Read more >
Worker background service Serilog problem - Microsoft Q&A
Hi, I have a worker background service that works as a Windows service on production. The problem is, it does not log errors...
Read more >
Application Insights for Worker Service using Serilog
This is a simple guide for implementing Azure Application Insights using Serilog in a dotnet core worker service. .Net Core worker services ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found