Serilog + WorkerService + IHostBuilder
See original GitHub issueI’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
- The current
IApplicationBuilder
is marked internal (https://github.com/getsentry/sentry-dotnet/blob/351f264afd569ca46ed180ea87a885c0fc212740/src/Sentry.AspNetCore/ApplicationBuilderExtensions.cs#L25) - The current
IServiceCollectionExtension
method is marked internal (https://github.com/getsentry/sentry-dotnet/blob/351f264afd569ca46ed180ea87a885c0fc212740/src/Sentry.AspNetCore/Extensions/DependencyInjection/ServiceCollectionExtensions.cs#L14) - Some classes used in these methods are also marked internal
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 methodUseSentryCore
that handles (1)
- Add
IWebHostBuilder
extension methodUseSentryWeb
that handles (2)
- Keep
IWebHostBuilder
extension methodUseSentry
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)
- https://github.com/getsentry/sentry-dotnet/blob/main/src/Sentry.AspNetCore/Extensions/DependencyInjection/ServiceCollectionExtensions.cs
- https://github.com/getsentry/sentry-dotnet/blob/main/src/Sentry.AspNetCore/SentryWebHostBuilderExtensions.cs
- https://github.com/getsentry/sentry-dotnet/blob/main/src/Sentry.AspNetCore/SentryAspNetCoreOptions.cs
- Optional, you could just add to the summary that some of the options have no effect in core
I might be able to free some time for a PR once this proposal is finalised
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (5 by maintainers)
Top GitHub Comments
Ok, thank you for the feedback. I’ll work on a first draft tomorrow
Blocked on #190. Keeping this open for followup after that is done.