Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Sentry.AspNetCore/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public static IApplicationBuilder UseSentry(this IApplicationBuilder app)
var lifetime = app.ApplicationServices.GetService<IHostApplicationLifetime>();
lifetime?.ApplicationStopped.Register(SentrySdk.Close);

_ = app.ApplicationServices.GetService<Func<IHub>>();

return app.UseMiddleware<SentryMiddleware>();
}
}
3 changes: 0 additions & 3 deletions src/Sentry.AspNetCore/SentryWebHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ public static IWebHostBuilder UseSentry(
this IWebHostBuilder builder,
Action<WebHostBuilderContext, ISentryBuilder>? 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Sentry.Extensions.Logging;

namespace Sentry.AspNetCore.Tests;

Expand All @@ -10,12 +12,13 @@ private class Fixture
public ISentryEventProcessor SentryEventProcessor { get; set; } = Substitute.For<ISentryEventProcessor>();
public ISentryEventExceptionProcessor SentryEventExceptionProcessor { get; set; } = Substitute.For<ISentryEventExceptionProcessor>();
public SentryAspNetCoreOptions SentryAspNetCoreOptions { get; set; } = new();
public IServiceProvider ServiceProvider { get; } = Substitute.For<IServiceProvider>();

public Dictionary<string, object> Properties { get; } = new();

public IApplicationBuilder GetSut()
{
var provider = Substitute.For<IServiceProvider>();
var provider = ServiceProvider;
var options = Substitute.For<IOptions<SentryAspNetCoreOptions>>();
_ = options.Value.Returns(SentryAspNetCoreOptions);
_ = provider.GetService(typeof(IOptions<SentryAspNetCoreOptions>)).Returns(options);
Expand Down Expand Up @@ -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<ISentryClient>))
.Returns(Substitute.For<ILogger<ISentryClient>>());
IDiagnosticLogger diagnosticLoggerWhenHubFactoryResolved = null;
_ = _fixture.ServiceProvider.GetService(typeof(Func<IHub>)).Returns(_ =>
{
diagnosticLoggerWhenHubFactoryResolved = _fixture.SentryAspNetCoreOptions.DiagnosticLogger;
return null;
});

_ = sut.UseSentry();

Assert.IsType<MelDiagnosticLogger>(diagnosticLoggerWhenHubFactoryResolved);
}

[Fact]
public void UseSentry_OriginalEventProcessor_StillAvailable()
{
Expand Down
48 changes: 36 additions & 12 deletions test/Sentry.AspNetCore.Tests/IntegrationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>());

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<UriFormatException>(() => Build());
}

private ILoggerFactory ReplaceLoggerFactory()
{
var logger = Substitute.For<ILogger>();
var factory = Substitute.For<ILoggerFactory>();
Expand All @@ -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<string>());

// 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;
}
}