diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs index 9888ab9a172..2d3f6d2c21a 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs @@ -947,6 +947,7 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan int numberOfUnSubscribedEvents = 0; int numberOfSubscribedEvents = 0; int numberOfExceptionCallbacks = 0; + bool exceptionHandled = false; // configure SDK this.tracerProvider = Sdk.CreateTracerProviderBuilder() @@ -991,18 +992,18 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan }) .Build(); + TestMiddleware.Create(builder => builder + .UseExceptionHandler(handler => + handler.Run(async (ctx) => + { + exceptionHandled = true; + await ctx.Response.WriteAsync("handled"); + }))); + using (var client = this.factory .WithWebHostBuilder(builder => { builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders()); - builder.Configure(app => app - .UseExceptionHandler(handler => - { - handler.Run(async (ctx) => - { - await ctx.Response.WriteAsync("handled"); - }); - })); }) .CreateClient()) { @@ -1020,6 +1021,7 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan Assert.Equal(0, numberOfExceptionCallbacks); Assert.Equal(0, numberOfUnSubscribedEvents); Assert.Equal(2, numberOfSubscribedEvents); + Assert.True(exceptionHandled); } public void Dispose() diff --git a/test/TestApp.AspNetCore/Program.cs b/test/TestApp.AspNetCore/Program.cs index 49b801fb6c3..06071eab4bd 100644 --- a/test/TestApp.AspNetCore/Program.cs +++ b/test/TestApp.AspNetCore/Program.cs @@ -47,6 +47,8 @@ public static void Main(string[] args) app.UseMiddleware(); + app.AddTestMiddleware(); + app.Run(); } } diff --git a/test/TestApp.AspNetCore/TestMiddleware.cs b/test/TestApp.AspNetCore/TestMiddleware.cs new file mode 100644 index 00000000000..39acf58db3d --- /dev/null +++ b/test/TestApp.AspNetCore/TestMiddleware.cs @@ -0,0 +1,24 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +namespace TestApp.AspNetCore; + +public static class TestMiddleware +{ + private static readonly AsyncLocal?> Current = new(); + + public static IApplicationBuilder AddTestMiddleware(this IApplicationBuilder builder) + { + if (Current.Value is { } configure) + { + configure(builder); + } + + return builder; + } + + public static void Create(Action action) + { + Current.Value = action; + } +}