diff --git a/src/Sentry.AspNetCore/ApplicationBuilderExtensions.cs b/src/Sentry.AspNetCore/ApplicationBuilderExtensions.cs index 6a92a06f40..89298259c9 100644 --- a/src/Sentry.AspNetCore/ApplicationBuilderExtensions.cs +++ b/src/Sentry.AspNetCore/ApplicationBuilderExtensions.cs @@ -44,6 +44,8 @@ public static IApplicationBuilder UseSentry(this IApplicationBuilder app) var lifetime = app.ApplicationServices.GetService(); lifetime?.ApplicationStopped.Register(SentrySdk.Close); + _ = app.ApplicationServices.GetService>(); + return app.UseMiddleware(); } } diff --git a/src/Sentry.AspNetCore/SentryWebHostBuilderExtensions.cs b/src/Sentry.AspNetCore/SentryWebHostBuilderExtensions.cs index 63545d07f9..45d8318b86 100644 --- a/src/Sentry.AspNetCore/SentryWebHostBuilderExtensions.cs +++ b/src/Sentry.AspNetCore/SentryWebHostBuilderExtensions.cs @@ -76,9 +76,6 @@ public static IWebHostBuilder UseSentry( this IWebHostBuilder builder, Action? configureSentry) { - // The earliest we can hook the SDK initialization code with the framework - // Initialization happens at a later time depending if the default MEL backend is enabled or not. - // In case the logging backend was replaced, init happens later, at the StartupFilter _ = builder.ConfigureLogging((context, logging) => { logging.AddConfiguration(); diff --git a/test/Sentry.AspNetCore.Tests/ApplicationBuilderExtensionsTests.cs b/test/Sentry.AspNetCore.Tests/ApplicationBuilderExtensionsTests.cs index dfe9d733c4..84f6599c74 100644 --- a/test/Sentry.AspNetCore.Tests/ApplicationBuilderExtensionsTests.cs +++ b/test/Sentry.AspNetCore.Tests/ApplicationBuilderExtensionsTests.cs @@ -1,5 +1,7 @@ using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Sentry.Extensions.Logging; namespace Sentry.AspNetCore.Tests; @@ -10,12 +12,13 @@ private class Fixture public ISentryEventProcessor SentryEventProcessor { get; set; } = Substitute.For(); public ISentryEventExceptionProcessor SentryEventExceptionProcessor { get; set; } = Substitute.For(); public SentryAspNetCoreOptions SentryAspNetCoreOptions { get; set; } = new(); + public IServiceProvider ServiceProvider { get; } = Substitute.For(); public Dictionary Properties { get; } = new(); public IApplicationBuilder GetSut() { - var provider = Substitute.For(); + var provider = ServiceProvider; var options = Substitute.For>(); _ = options.Value.Returns(SentryAspNetCoreOptions); _ = provider.GetService(typeof(IOptions)).Returns(options); @@ -61,6 +64,25 @@ public void UseSentry_DiagnosticSet_NoOverriden() Assert.Same(diagnosticLogger, _fixture.SentryAspNetCoreOptions.DiagnosticLogger); } + [Fact] + public void UseSentry_DebugEnabled_ResolvesHubFactoryAfterSettingDiagnosticLogger() + { + _fixture.SentryAspNetCoreOptions.Debug = true; + var sut = _fixture.GetSut(); + _ = _fixture.ServiceProvider.GetService(typeof(ILogger)) + .Returns(Substitute.For>()); + IDiagnosticLogger diagnosticLoggerWhenHubFactoryResolved = null; + _ = _fixture.ServiceProvider.GetService(typeof(Func)).Returns(_ => + { + diagnosticLoggerWhenHubFactoryResolved = _fixture.SentryAspNetCoreOptions.DiagnosticLogger; + return null; + }); + + _ = sut.UseSentry(); + + Assert.IsType(diagnosticLoggerWhenHubFactoryResolved); + } + [Fact] public void UseSentry_OriginalEventProcessor_StillAvailable() { diff --git a/test/Sentry.AspNetCore.Tests/IntegrationsTests.cs b/test/Sentry.AspNetCore.Tests/IntegrationsTests.cs index 2f634c5975..b7b4888690 100644 --- a/test/Sentry.AspNetCore.Tests/IntegrationsTests.cs +++ b/test/Sentry.AspNetCore.Tests/IntegrationsTests.cs @@ -162,6 +162,41 @@ public async Task UnhandledException_AvailableThroughLastExceptionFilter() [Fact] public async Task WebHost_SdkStartedViaStartupFilter_DisableSdkOnShutdown() + { + var factory = ReplaceLoggerFactory(); + + Build(); + + // Make sure custom factory was used instead of default one + _ = factory.Received().CreateLogger(Arg.Any()); + + Assert.True(SentrySdk.IsEnabled); + + await TestServer.Host.StopAsync(); + Assert.False(SentrySdk.IsEnabled); + } + + [Fact] + public void WebHost_LoggerFactoryReplacedInitializeSdkFalse_SdkDisabled() + { + Configure = o => o.InitializeSdk = false; + _ = ReplaceLoggerFactory(); + + Build(); + + Assert.False(SentrySdk.IsEnabled); + } + + [Fact] + public void WebHost_LoggerFactoryReplacedInvalidDsn_StartThrows() + { + Configure = o => o.Dsn = "invalid dsn"; + _ = ReplaceLoggerFactory(); + + Assert.Throws(() => Build()); + } + + private ILoggerFactory ReplaceLoggerFactory() { var logger = Substitute.For(); var factory = Substitute.For(); @@ -178,17 +213,6 @@ public async Task WebHost_SdkStartedViaStartupFilter_DisableSdkOnShutdown() collection.AddSingleton(factory)); }; - Build(); - - // Make sure custom factory was used instead of default one - _ = factory.Received().CreateLogger(Arg.Any()); - - // Invoke request to make sure Sentry is initialized - await HttpClient.GetAsync("/"); - - Assert.True(SentrySdk.IsEnabled); - - await TestServer.Host.StopAsync(); - Assert.False(SentrySdk.IsEnabled); + return factory; } }