From e3a75cf54a9d417ad567f07f72ce41c490a47d47 Mon Sep 17 00:00:00 2001 From: Ricardo Colombo Oliveira Date: Tue, 13 Oct 2026 15:39:06 +0200 Subject: [PATCH] feat(aspnetcore): add UseSentry overloads on WebApplicationBuilder Minimal-hosting apps had to call builder.WebHost.UseSentry(...), which returns the IWebHostBuilder. Chained, the following .Build() is IWebHostBuilder.Build(): ASPDEPR008 on .NET 10, CS1929 on MapGet, and a NotSupportedException at runtime. Each new overload calls its builder.WebHost.UseSentry(...) twin and returns the WebApplicationBuilder, so behavior matches the existing call. The ASP.NET Core samples now use builder.UseSentry(...). Closes #5719 Co-Authored-By: Claude Opus 5.5 --- .../Program.cs | 2 +- .../Program.cs | 2 +- .../Sentry.Samples.AspNetCore.Mvc/Program.cs | 2 +- .../Program.cs | 4 +- .../Program.cs | 2 +- .../Sentry.Samples.GraphQL.Server/Program.cs | 2 +- .../Program.cs | 2 +- .../Program.cs | 2 +- .../SentryWebApplicationBuilderExtensions.cs | 59 ++++++ ...iApprovalTests.Run.DotNet10_0.verified.txt | 7 + ...piApprovalTests.Run.DotNet8_0.verified.txt | 7 + ...piApprovalTests.Run.DotNet9_0.verified.txt | 7 + ...tryWebApplicationBuilderExtensionsTests.cs | 168 ++++++++++++++++++ 13 files changed, 257 insertions(+), 9 deletions(-) create mode 100644 src/Sentry.AspNetCore/SentryWebApplicationBuilderExtensions.cs create mode 100644 test/Sentry.AspNetCore.Tests/SentryWebApplicationBuilderExtensionsTests.cs diff --git a/samples/Sentry.Samples.AspNetCore.Basic/Program.cs b/samples/Sentry.Samples.AspNetCore.Basic/Program.cs index 96788a87c0..28266bd9c2 100644 --- a/samples/Sentry.Samples.AspNetCore.Basic/Program.cs +++ b/samples/Sentry.Samples.AspNetCore.Basic/Program.cs @@ -1,6 +1,6 @@ var builder = WebApplication.CreateBuilder(args); -builder.WebHost.UseSentry(options => +builder.UseSentry(options => { #if !SENTRY_DSN_DEFINED_IN_ENV // A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable. diff --git a/samples/Sentry.Samples.AspNetCore.Blazor.Server/Program.cs b/samples/Sentry.Samples.AspNetCore.Blazor.Server/Program.cs index dda6c4779c..3fe824d875 100644 --- a/samples/Sentry.Samples.AspNetCore.Blazor.Server/Program.cs +++ b/samples/Sentry.Samples.AspNetCore.Blazor.Server/Program.cs @@ -32,7 +32,7 @@ }); #endif -builder.WebHost.UseSentry(options => +builder.UseSentry(options => { options.Dsn = dsn; #if NET10_0_OR_GREATER diff --git a/samples/Sentry.Samples.AspNetCore.Mvc/Program.cs b/samples/Sentry.Samples.AspNetCore.Mvc/Program.cs index 883d0c3aa3..3a914dc689 100644 --- a/samples/Sentry.Samples.AspNetCore.Mvc/Program.cs +++ b/samples/Sentry.Samples.AspNetCore.Mvc/Program.cs @@ -9,7 +9,7 @@ // configuration system, which includes anything defined on the ConfigurationBuilder ('appsettings.json', environment // variables etc.) // See: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-8.0 -builder.WebHost.UseSentry(options => +builder.UseSentry(options => { #if !SENTRY_DSN_DEFINED_IN_ENV // A DSN is required. You can set here in code, via the SENTRY_DSN environment variable or in your diff --git a/samples/Sentry.Samples.AspNetCore.Serilog/Program.cs b/samples/Sentry.Samples.AspNetCore.Serilog/Program.cs index 65d3d64732..8a41b194bd 100644 --- a/samples/Sentry.Samples.AspNetCore.Serilog/Program.cs +++ b/samples/Sentry.Samples.AspNetCore.Serilog/Program.cs @@ -32,9 +32,9 @@ public static WebApplication BuildWebApp(string[] args) // or coded explicitly, via parameter like: // .UseSentry("dsn") or .UseSentry(o => o.Dsn = ""; o.Release = "1.0"; ...) #if !SENTRY_DSN_DEFINED_IN_ENV - builder.WebHost.UseSentry(SamplesShared.Dsn); + builder.UseSentry(SamplesShared.Dsn); #else - builder.WebHost.UseSentry(EnvironmentVariables.Dsn); + builder.UseSentry(EnvironmentVariables.Dsn); #endif // The App: diff --git a/samples/Sentry.Samples.AspNetCore.WebAPI.Profiling/Program.cs b/samples/Sentry.Samples.AspNetCore.WebAPI.Profiling/Program.cs index 673b3d7f57..7c95631baf 100644 --- a/samples/Sentry.Samples.AspNetCore.WebAPI.Profiling/Program.cs +++ b/samples/Sentry.Samples.AspNetCore.WebAPI.Profiling/Program.cs @@ -1,6 +1,6 @@ var builder = WebApplication.CreateBuilder(args); -builder.WebHost.UseSentry(o => +builder.UseSentry(o => { #if !SENTRY_DSN_DEFINED_IN_ENV // A DSN is required. You can set here in code, in the SENTRY_DSN environment variable or in your appsettings.json diff --git a/samples/Sentry.Samples.GraphQL.Server/Program.cs b/samples/Sentry.Samples.GraphQL.Server/Program.cs index 5eb4f9f537..8a435a12e2 100644 --- a/samples/Sentry.Samples.GraphQL.Server/Program.cs +++ b/samples/Sentry.Samples.GraphQL.Server/Program.cs @@ -48,7 +48,7 @@ public static WebApplication BuildWebApplication(string[] args) .AddSentryOtlpExporter(dsn) // <-- Ensure telemetry is sent to Sentry ); - builder.WebHost.UseSentry(options => + builder.UseSentry(options => { options.Dsn = dsn; diff --git a/samples/Sentry.Samples.ME.AI.AspNetCore/Program.cs b/samples/Sentry.Samples.ME.AI.AspNetCore/Program.cs index d20512aa33..972bb22386 100644 --- a/samples/Sentry.Samples.ME.AI.AspNetCore/Program.cs +++ b/samples/Sentry.Samples.ME.AI.AspNetCore/Program.cs @@ -2,7 +2,7 @@ var builder = WebApplication.CreateBuilder(args); -builder.WebHost.UseSentry(options => +builder.UseSentry(options => { #if !SENTRY_DSN_DEFINED_IN_ENV // A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable. diff --git a/samples/Sentry.Samples.OpenTelemetry.AspNetCore/Program.cs b/samples/Sentry.Samples.OpenTelemetry.AspNetCore/Program.cs index c414c6e862..3b7496a835 100644 --- a/samples/Sentry.Samples.OpenTelemetry.AspNetCore/Program.cs +++ b/samples/Sentry.Samples.OpenTelemetry.AspNetCore/Program.cs @@ -34,7 +34,7 @@ .AddSentryOtlpExporter(dsn) ); -builder.WebHost.UseSentry(options => +builder.UseSentry(options => { options.Dsn = dsn; options.Debug = builder.Environment.IsDevelopment(); diff --git a/src/Sentry.AspNetCore/SentryWebApplicationBuilderExtensions.cs b/src/Sentry.AspNetCore/SentryWebApplicationBuilderExtensions.cs new file mode 100644 index 0000000000..011dc0718b --- /dev/null +++ b/src/Sentry.AspNetCore/SentryWebApplicationBuilderExtensions.cs @@ -0,0 +1,59 @@ +using Microsoft.AspNetCore.Hosting; +using Sentry.AspNetCore; + +// ReSharper disable once CheckNamespace +namespace Microsoft.AspNetCore.Builder; + +/// +/// Extension methods to +/// +[EditorBrowsable(EditorBrowsableState.Never)] +public static class SentryWebApplicationBuilderExtensions +{ + /// + /// Uses Sentry integration. + /// + /// The builder. + public static WebApplicationBuilder UseSentry(this WebApplicationBuilder builder) + { + builder.WebHost.UseSentry(); + return builder; + } + + /// + /// Uses Sentry integration. + /// + /// The builder. + /// The DSN. + public static WebApplicationBuilder UseSentry(this WebApplicationBuilder builder, string dsn) + { + builder.WebHost.UseSentry(dsn); + return builder; + } + + /// + /// Uses Sentry integration. + /// + /// The builder. + /// The configure options. + public static WebApplicationBuilder UseSentry( + this WebApplicationBuilder builder, + Action? configureOptions) + { + builder.WebHost.UseSentry(configureOptions); + return builder; + } + + /// + /// Uses Sentry integration. + /// + /// The builder. + /// The Sentry builder. + public static WebApplicationBuilder UseSentry( + this WebApplicationBuilder builder, + Action? configureSentry) + { + builder.WebHost.UseSentry(configureSentry); + return builder; + } +} diff --git a/test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet10_0.verified.txt b/test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet10_0.verified.txt index f6ceb2a3c3..6704f3a05e 100644 --- a/test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet10_0.verified.txt +++ b/test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet10_0.verified.txt @@ -4,6 +4,13 @@ { public static Microsoft.AspNetCore.Builder.IApplicationBuilder UseSentryTracing(this Microsoft.AspNetCore.Builder.IApplicationBuilder builder) { } } + public static class SentryWebApplicationBuilderExtensions + { + public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder) { } + public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, System.Action? configureSentry) { } + public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, System.Action? configureOptions) { } + public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, string dsn) { } + } } namespace Microsoft.AspNetCore.Hosting { diff --git a/test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt b/test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt index f6ceb2a3c3..6704f3a05e 100644 --- a/test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt +++ b/test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt @@ -4,6 +4,13 @@ { public static Microsoft.AspNetCore.Builder.IApplicationBuilder UseSentryTracing(this Microsoft.AspNetCore.Builder.IApplicationBuilder builder) { } } + public static class SentryWebApplicationBuilderExtensions + { + public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder) { } + public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, System.Action? configureSentry) { } + public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, System.Action? configureOptions) { } + public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, string dsn) { } + } } namespace Microsoft.AspNetCore.Hosting { diff --git a/test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet9_0.verified.txt b/test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet9_0.verified.txt index f6ceb2a3c3..6704f3a05e 100644 --- a/test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet9_0.verified.txt +++ b/test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet9_0.verified.txt @@ -4,6 +4,13 @@ { public static Microsoft.AspNetCore.Builder.IApplicationBuilder UseSentryTracing(this Microsoft.AspNetCore.Builder.IApplicationBuilder builder) { } } + public static class SentryWebApplicationBuilderExtensions + { + public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder) { } + public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, System.Action? configureSentry) { } + public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, System.Action? configureOptions) { } + public static Microsoft.AspNetCore.Builder.WebApplicationBuilder UseSentry(this Microsoft.AspNetCore.Builder.WebApplicationBuilder builder, string dsn) { } + } } namespace Microsoft.AspNetCore.Hosting { diff --git a/test/Sentry.AspNetCore.Tests/SentryWebApplicationBuilderExtensionsTests.cs b/test/Sentry.AspNetCore.Tests/SentryWebApplicationBuilderExtensionsTests.cs new file mode 100644 index 0000000000..a8f166a8c5 --- /dev/null +++ b/test/Sentry.AspNetCore.Tests/SentryWebApplicationBuilderExtensionsTests.cs @@ -0,0 +1,168 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace Sentry.AspNetCore.Tests; + +[Collection(nameof(SentrySdkCollection))] +public class SentryWebApplicationBuilderExtensionsTests : IDisposable +{ + public void Dispose() => SentrySdk.Close(); + + private static WebApplicationBuilder CreateBuilder( + string environmentName = "Production", + params (string Key, string Value)[] settings) + { + var builder = WebApplication.CreateBuilder(new WebApplicationOptions { EnvironmentName = environmentName }); + ((IConfigurationBuilder)builder.Configuration).AddInMemoryCollection( + settings.ToDictionary(s => s.Key, s => s.Value)); + return builder; + } + + private static SentryAspNetCoreOptions ResolveOptions(WebApplicationBuilder builder) + { + using var services = builder.Services.BuildServiceProvider(); + return services.GetRequiredService>().Value; + } + + private static void DoNotSend(SentryAspNetCoreOptions options) + { + options.Dsn = ValidDsn; + options.BackgroundWorker = Substitute.For(); + options.AutoSessionTracking = false; + options.InitNativeSdks = false; + } + + public static TheoryData> Overloads => new() + { + { "parameterless", b => b.UseSentry() }, + { "dsn", b => b.UseSentry(ValidDsn) }, + { "options callback", b => b.UseSentry((SentryAspNetCoreOptions o) => DoNotSend(o)) }, + { "sentry builder callback", b => b.UseSentry((ISentryBuilder s) => s.AddSentryOptions(DoNotSend)) }, + }; + + [Theory, MemberData(nameof(Overloads))] + public void UseSentry_AnyOverload_RegistersAspNetCoreIntegration( + string overload, Func useSentry) + { + _ = overload; + var builder = CreateBuilder(); + + var returned = useSentry(builder); + + returned.Should().BeSameAs(builder); + builder.Services.Should().ContainSingle(d => d.ServiceType == typeof(IHub)); + builder.Services.Should().ContainSingle(d => d.ImplementationType == typeof(SentryStartupFilter)); + builder.Services.Should().ContainSingle(d => d.ImplementationType == typeof(SentryTracingStartupFilter)); + builder.Services.Should().ContainSingle(d => d.ImplementationType == typeof(SentryMiddleware)); + builder.Services.Should().ContainSingle(d => d.ImplementationType == typeof(SentryAspNetCoreLoggerProvider)); + builder.Services.Should().ContainSingle(d => d.ImplementationType == typeof(SentryAspNetCoreStructuredLoggerProvider)); + } + + [Fact] + public void UseSentry_SentryAndLoggingSections_BindsOptions() + { + var builder = CreateBuilder(settings: new[] + { + ("Sentry:Dsn", ValidDsn), + ("Sentry:SendDefaultPii", "true"), + ("Logging:Sentry:MinimumEventLevel", "Warning"), + }); + + builder.UseSentry(); + + var options = ResolveOptions(builder); + options.Dsn.Should().Be(ValidDsn); + options.SendDefaultPii.Should().BeTrue(); + options.MinimumEventLevel.Should().Be(LogLevel.Warning); + } + + [Fact] + public void UseSentry_Dsn_SetsDsn() + { + var builder = CreateBuilder(); + + builder.UseSentry(ValidDsn); + + ResolveOptions(builder).Dsn.Should().Be(ValidDsn); + } + + [Fact] + public void UseSentry_SentryBuilderCallback_ConfiguresOptions() + { + var builder = CreateBuilder(); + + builder.UseSentry((ISentryBuilder sentryBuilder) => + sentryBuilder.AddSentryOptions(options => options.SendDefaultPii = true)); + + ResolveOptions(builder).SendDefaultPii.Should().BeTrue(); + } + + [Fact] + public void UseSentry_OptionsCallback_RunsAfterConfigurationBinding() + { + var builder = CreateBuilder(settings: ("Sentry:SendDefaultPii", "true")); + + builder.UseSentry(options => options.SendDefaultPii = false); + + ResolveOptions(builder).SendDefaultPii.Should().BeFalse(); + } + + [Fact] + public void UseSentry_NoEnvironmentConfigured_UsesHostEnvironment() + { + var builder = CreateBuilder("Staging"); + + builder.UseSentry(); + + ResolveOptions(builder).Environment.Should().Be("staging"); + } + + [Fact] + public void UseSentry_AdjustStandardEnvironmentNameCasingInConfiguration_KeepsHostEnvironmentCasing() + { + var builder = CreateBuilder("Staging", ("Sentry:AdjustStandardEnvironmentNameCasing", "false")); + + builder.UseSentry(); + + ResolveOptions(builder).Environment.Should().Be("Staging"); + } + + [Fact] + public void UseSentry_EnvironmentSetInCallback_KeepsEnvironment() + { + var builder = CreateBuilder("Staging"); + + builder.UseSentry(options => options.Environment = "from-code"); + + ResolveOptions(builder).Environment.Should().Be("from-code"); + } + + [Fact] + public async Task UseSentry_UnhandledRequestException_CapturedBySentryMiddleware() + { + var events = new List(); + var builder = CreateBuilder(); + builder.WebHost.UseTestServer(); + builder.UseSentry(options => + { + DoNotSend(options); + options.SetBeforeSend((e, _) => + { + events.Add(e); + return null; + }); + }); + await using var app = builder.Build(); + app.MapGet("/throw", string () => throw new InvalidOperationException("thrown from an endpoint")); + await app.StartAsync(); + + _ = await Record.ExceptionAsync(() => app.GetTestClient().GetAsync("/throw")); + + events.Should().ContainSingle(e => e.Exception!.Message == "thrown from an endpoint"); + } +}