Skip to content

Commit 4f33571

Browse files
niemyjskiCopilot
andcommitted
feat(aspnetcore): auto-register IExceptionHandler in WebApplicationBuilder.AddExceptionless
AddExceptionless() on WebApplicationBuilder now automatically calls AddExceptionlessExceptionHandler(), eliminating the need for a separate registration call. The standalone AddExceptionlessExceptionHandler() method remains public for non-WebApplicationBuilder scenarios. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3b2dc85 commit 4f33571

3 files changed

Lines changed: 38 additions & 7 deletions

File tree

samples/Exceptionless.SampleAspNetCore/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
builder.Logging.AddExceptionless();
1111

1212
// Reads settings from IConfiguration then adds additional configuration from this lambda.
13-
// This also configures ExceptionlessClient.Default and host shutdown queue flushing.
13+
// This also configures ExceptionlessClient.Default, host shutdown queue flushing,
14+
// and automatically registers the Exceptionless IExceptionHandler.
1415
builder.AddExceptionless(c => c.DefaultData["Startup"] = "heyyy");
1516
// OR
1617
// builder.AddExceptionless();
1718
// OR
1819
// builder.AddExceptionless("API_KEY_HERE");
1920

20-
// Adds ASP.NET Core request/unhandled exception hooks and standard exception handling services.
21-
builder.Services.AddExceptionlessExceptionHandler();
21+
// Required: configures the response format for unhandled exceptions (e.g., RFC 7807 Problem Details).
2222
builder.Services.AddProblemDetails();
2323

2424
// This is normal ASP.NET Core code.

src/Platforms/Exceptionless.AspNetCore/ExceptionlessExtensions.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,40 @@
1414

1515
namespace Exceptionless {
1616
public static class ExceptionlessExtensions {
17+
/// <summary>
18+
/// Adds the given pre-configured <see cref="ExceptionlessClient"/> to the web application builder,
19+
/// registers lifecycle hooks, and automatically registers the Exceptionless <see cref="IExceptionHandler"/>.
20+
/// </summary>
21+
public static WebApplicationBuilder AddExceptionless(this WebApplicationBuilder builder, ExceptionlessClient client) {
22+
((IHostApplicationBuilder)builder).AddExceptionless(client);
23+
builder.Services.AddExceptionlessExceptionHandler();
24+
return builder;
25+
}
26+
27+
/// <summary>
28+
/// Adds an <see cref="ExceptionlessClient"/> to the web application builder using the specified API key,
29+
/// registers lifecycle hooks, and automatically registers the Exceptionless <see cref="IExceptionHandler"/>.
30+
/// </summary>
31+
public static WebApplicationBuilder AddExceptionless(this WebApplicationBuilder builder, string apiKey) {
32+
((IHostApplicationBuilder)builder).AddExceptionless(apiKey);
33+
builder.Services.AddExceptionlessExceptionHandler();
34+
return builder;
35+
}
36+
37+
/// <summary>
38+
/// Adds an <see cref="ExceptionlessClient"/> to the web application builder using configuration,
39+
/// registers lifecycle hooks, and automatically registers the Exceptionless <see cref="IExceptionHandler"/>.
40+
/// </summary>
41+
public static WebApplicationBuilder AddExceptionless(this WebApplicationBuilder builder, Action<ExceptionlessConfiguration> configure = null) {
42+
((IHostApplicationBuilder)builder).AddExceptionless(configure);
43+
builder.Services.AddExceptionlessExceptionHandler();
44+
return builder;
45+
}
46+
1747
/// <summary>
1848
/// Registers the Exceptionless <see cref="IExceptionHandler"/> and required ASP.NET Core services
1949
/// for capturing unhandled exceptions. Call this in your service configuration alongside <c>app.UseExceptionHandler()</c>.
50+
/// This is called automatically when using the <see cref="WebApplicationBuilder"/> overloads of <c>AddExceptionless</c>.
2051
/// </summary>
2152
public static IServiceCollection AddExceptionlessExceptionHandler(this IServiceCollection services) {
2253
services.AddHttpContextAccessor();

src/Platforms/Exceptionless.AspNetCore/readme.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ using Exceptionless;
3232

3333
var builder = WebApplication.CreateBuilder(args);
3434
builder.AddExceptionless(c => c.ApiKey = "API_KEY_HERE");
35-
builder.Services.AddExceptionlessExceptionHandler();
3635
builder.Services.AddProblemDetails();
3736

38-
In order to start gathering unhandled exceptions, you will need to register the Exceptionless middleware in your application
39-
like this after building your application:
40-
4137
var app = builder.Build();
4238
app.UseExceptionHandler();
4339
app.UseExceptionless();
4440

41+
The `AddExceptionless` call on `WebApplicationBuilder` automatically registers the Exceptionless IExceptionHandler
42+
for capturing unhandled exceptions. You still need `AddProblemDetails()` (or your own handler) to produce the HTTP
43+
error response, and `UseExceptionHandler()` to enable the exception handling pipeline.
44+
4545
Alternatively, you can use different overloads of the host builder AddExceptionless method for other configuration options.
4646
Please visit the documentation at https://exceptionless.com/docs/clients/dotnet/sending-events/ for additional examples
4747
and guidance on sending events to Exceptionless.

0 commit comments

Comments
 (0)