|
| 1 | +# Exceptionless.AspNetCore Client |
| 2 | + |
| 3 | + |
| 4 | +## When To Use |
| 5 | + |
| 6 | +Use `Exceptionless.AspNetCore` for ASP.NET Core web apps and APIs. It integrates with DI, ASP.NET Core exception handling, request diagnostics, 404 tracking, HTTP context collection, and host shutdown flushing. Add `Exceptionless.Extensions.Logging` when `ILogger` events should be sent too. |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +```bash |
| 11 | +dotnet add package Exceptionless.AspNetCore |
| 12 | +dotnet add package Exceptionless.Extensions.Logging |
| 13 | +``` |
| 14 | + |
| 15 | +## Current Minimal Setup |
| 16 | + |
| 17 | +Current source setup is newer than the public web-server example. Use `WebApplicationBuilder.AddExceptionless`, `AddProblemDetails`, `UseExceptionHandler`, and `UseExceptionless`. |
| 18 | + |
| 19 | +```csharp |
| 20 | +using Exceptionless; |
| 21 | +using Microsoft.AspNetCore.Builder; |
| 22 | +using Microsoft.Extensions.DependencyInjection; |
| 23 | +using Microsoft.Extensions.Logging; |
| 24 | + |
| 25 | +var builder = WebApplication.CreateBuilder(args); |
| 26 | + |
| 27 | +builder.Logging.AddExceptionless(); |
| 28 | +builder.AddExceptionless(c => { |
| 29 | + c.ApiKey = "VALID_API_KEY_12345"; |
| 30 | + c.DefaultData["Service"] = "payments-api"; |
| 31 | + c.DefaultTags.Add("aspnetcore"); |
| 32 | +}); |
| 33 | + |
| 34 | +builder.Services.AddProblemDetails(); |
| 35 | +builder.Services.AddControllers(); |
| 36 | + |
| 37 | +var app = builder.Build(); |
| 38 | + |
| 39 | +app.UseExceptionHandler(); |
| 40 | +app.UseExceptionless(); |
| 41 | + |
| 42 | +app.MapControllers(); |
| 43 | +app.Run(); |
| 44 | +``` |
| 45 | + |
| 46 | +## Minimal APIs |
| 47 | + |
| 48 | +Minimal APIs are supported using the same middleware stack as controllers; register `ProblemDetails`, `UseExceptionHandler`, and `UseExceptionless` before endpoint mapping, and inject `ExceptionlessClient` into handlers as needed. |
| 49 | + |
| 50 | +```csharp |
| 51 | +using Exceptionless; |
| 52 | +using Exceptionless.AspNetCore; |
| 53 | +using Microsoft.AspNetCore.Builder; |
| 54 | +using Microsoft.AspNetCore.Http; |
| 55 | +using Microsoft.Extensions.DependencyInjection; |
| 56 | + |
| 57 | +var builder = WebApplication.CreateBuilder(args); |
| 58 | + |
| 59 | +builder.Logging.AddExceptionless(); |
| 60 | +builder.AddExceptionless(c => { |
| 61 | + c.ApiKey = "VALID_API_KEY_12345"; |
| 62 | + c.DefaultData["Service"] = "minimal-api"; |
| 63 | + c.DefaultTags.Add("minimal-api"); |
| 64 | +}); |
| 65 | + |
| 66 | +builder.Services.AddProblemDetails(); |
| 67 | + |
| 68 | +var app = builder.Build(); |
| 69 | + |
| 70 | +app.UseExceptionHandler(); |
| 71 | +app.UseExceptionless(); |
| 72 | + |
| 73 | +app.MapGet("/ping", (ExceptionlessClient exceptionless) => { |
| 74 | + exceptionless.SubmitFeatureUsage("PingEndpoint"); |
| 75 | + return Results.Ok(new { ok = true, service = "minimal-api" }); |
| 76 | +}); |
| 77 | + |
| 78 | +app.MapGet("/fail", (HttpContext context) => throw new InvalidOperationException("Handled by middleware")); |
| 79 | + |
| 80 | +app.Run(); |
| 81 | +``` |
| 82 | + |
| 83 | +## Controller And Service Usage |
| 84 | + |
| 85 | +Inject `ExceptionlessClient`. The static default works, but DI is better for tests and code clarity. |
| 86 | + |
| 87 | +```csharp |
| 88 | +using System; |
| 89 | +using Exceptionless; |
| 90 | +using Microsoft.AspNetCore.Mvc; |
| 91 | +using Microsoft.Extensions.Logging; |
| 92 | + |
| 93 | +[ApiController] |
| 94 | +[Route("api/[controller]")] |
| 95 | +public sealed class ValuesController : ControllerBase { |
| 96 | + private readonly ExceptionlessClient _exceptionless; |
| 97 | + private readonly ILogger<ValuesController> _logger; |
| 98 | + |
| 99 | + public ValuesController(ExceptionlessClient exceptionless, ILogger<ValuesController> logger) { |
| 100 | + _exceptionless = exceptionless; |
| 101 | + _logger = logger; |
| 102 | + } |
| 103 | + |
| 104 | + [HttpGet("{id}")] |
| 105 | + public IActionResult Get(string id) { |
| 106 | + _logger.LogWarning("Loading value {ValueId}", id); |
| 107 | + _exceptionless.SubmitFeatureUsage("ValuesController.Get"); |
| 108 | + |
| 109 | + try { |
| 110 | + throw new InvalidOperationException("Example handled failure."); |
| 111 | + } catch (Exception ex) { |
| 112 | + ex.ToExceptionless(_exceptionless) |
| 113 | + .SetProperty("ValueId", id) |
| 114 | + .AddTags("handled") |
| 115 | + .Submit(); |
| 116 | + } |
| 117 | + |
| 118 | + return Ok(new { id }); |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +## Middleware Behavior |
| 124 | + |
| 125 | +`app.UseExceptionless()`: |
| 126 | + |
| 127 | +- Resolves the DI client or falls back to `ExceptionlessClient.Default`. |
| 128 | +- Calls `client.Startup()`. |
| 129 | +- Adds `ExceptionlessAspNetCorePlugin` and `IgnoreUserAgentPlugin`. |
| 130 | +- Subscribes to relevant diagnostic listener events. |
| 131 | +- Registers shutdown queue flushing if the hosting lifetime service is not already registered. |
| 132 | +- Adds middleware that records 404 responses. |
| 133 | +- Flushes the queue on response completion when `ProcessQueueOnCompletedRequest` is true. |
| 134 | + |
| 135 | +## ASP.NET Core Configuration |
| 136 | + |
| 137 | +```json |
| 138 | +{ |
| 139 | + "Exceptionless": { |
| 140 | + "ApiKey": "VALID_API_KEY_12345", |
| 141 | + "ServerUrl": "https://collector.exceptionless.io", |
| 142 | + "IncludePrivateInformation": false, |
| 143 | + "ProcessQueueOnCompletedRequest": false, |
| 144 | + "DefaultData": { |
| 145 | + "Service": "payments-api" |
| 146 | + }, |
| 147 | + "DefaultTags": [ "aspnetcore", "production" ], |
| 148 | + "Settings": { |
| 149 | + "@@log:*": "Warn", |
| 150 | + "enableLogSubmission": true |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +## Serverless ASP.NET Core |
| 157 | + |
| 158 | +For AWS Lambda/Azure Functions style ASP.NET Core hosting, set `ProcessQueueOnCompletedRequest` when each request must flush before the runtime freezes. This is safer but more expensive per request. |
| 159 | + |
| 160 | +## Best Practices |
| 161 | + |
| 162 | +- Keep `UseExceptionHandler()` before endpoints; call `UseExceptionless()` before endpoint mapping. |
| 163 | +- Add `AddProblemDetails()` or a custom exception response handler so HTTP errors get responses. |
| 164 | +- Prefer `ILogger` for normal application logs and `ExceptionlessClient` for feature usage, not-found, custom events, and handled exceptions with rich context. |
| 165 | +- Include privacy configuration in `appsettings.json`, not only in code. |
| 166 | +- Use `SetHttpContext` for manual events outside the normal request pipeline when request info matters. |
0 commit comments