Skip to content

Commit 9d467f1

Browse files
fix: UseSentry now initializes Sentry even when Logging.AddSentry ran first
The logging integration registers a non-initializing Func<IHub>, and the initializing one was registered with TryAdd, so whichever ran first won. Calling builder.Logging.AddSentry() before UseSentry therefore left the SDK disabled with no indication, where v6 failed loudly at startup with "You must supply a DSN". The initializing registration now replaces any existing accessor, so the order of the two calls no longer matters. Tests cover both orders; the AddSentry-first one fails without this change. Calling both is still redundant and still registers two pairs of logger providers, which is unchanged from v6. Reported by Cursor Bugbot on #5595. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent dfcb80c commit 9d467f1

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎src/Sentry.Extensions.Logging/Extensions/DependencyInjection/ServiceCollectionExtensions.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ internal static IServiceCollection AddSentry<TOptions>(this IServiceCollection s
2929

3030
if (initializeSdk)
3131
{
32-
services.TryAddSingleton<Func<IHub>>(c =>
32+
// Replaces rather than TryAdd: the logging integration registers a non-initializing accessor, and
33+
// whichever is registered first would otherwise win. Initializing has to take precedence.
34+
services.RemoveAll<Func<IHub>>();
35+
services.AddSingleton<Func<IHub>>(c =>
3336
{
3437
var options = c.GetRequiredService<IOptions<TOptions>>().Value;
3538
var hub = SentrySdk.InitHub(options);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#if NET6_0_OR_GREATER
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Sentry.AspNetCore.Tests;
8+
9+
// Calling both UseSentry and Logging.AddSentry is redundant, but reachable. Whichever registered the hub
10+
// accessor first used to win, so calling AddSentry first left the SDK disabled.
11+
[Collection(nameof(SentrySdkCollection))]
12+
public class UseSentryWithLoggingAddSentryTests : IDisposable
13+
{
14+
public void Dispose() => SentrySdk.Close();
15+
16+
private static IHub BuildHub(bool useSentryFirst)
17+
{
18+
var builder = WebApplication.CreateBuilder();
19+
20+
void UseSentry() => builder.WebHost.UseSentry((SentryAspNetCoreOptions options) =>
21+
{
22+
options.Dsn = ValidDsn;
23+
options.BackgroundWorker = Substitute.For<IBackgroundWorker>();
24+
options.AutoSessionTracking = false;
25+
options.InitNativeSdks = false;
26+
});
27+
28+
if (useSentryFirst)
29+
{
30+
UseSentry();
31+
builder.Logging.AddSentry();
32+
}
33+
else
34+
{
35+
builder.Logging.AddSentry();
36+
UseSentry();
37+
}
38+
39+
using var app = builder.Build();
40+
return app.Services.GetRequiredService<IHub>();
41+
}
42+
43+
[Theory]
44+
[InlineData(true)]
45+
[InlineData(false)]
46+
public void UseSentry_WithLoggingAddSentry_InitializesSdk(bool useSentryFirst)
47+
=> Assert.True(BuildHub(useSentryFirst).IsEnabled);
48+
}
49+
#endif

0 commit comments

Comments
 (0)