Skip to content

Commit faa8e0a

Browse files
committed
fix: log exceptions
1 parent a410a7d commit faa8e0a

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

WebApi/AuthHandlers/ApiKeyAuthHandler.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,29 @@ public class ApiKeyAuthSchemeOptions : AuthenticationSchemeOptions
2020

2121
public class ApiKeyAuthHandler : AuthenticationHandler<ApiKeyAuthSchemeOptions>
2222
{
23+
private readonly ILogger<ApiKeyAuthHandler> _logger;
2324
private readonly ApiKeyProvider _apiKeyProvider;
2425
private readonly UserCache _userCache;
2526
private readonly Regex _apiKeyMatch = new(@"^Key (?<key>.*)$", RegexOptions.Compiled);
2627

2728
/// <inheritdoc />
2829
public ApiKeyAuthHandler(
30+
ILogger<ApiKeyAuthHandler> logger,
2931
IOptionsMonitor<ApiKeyAuthSchemeOptions> options,
30-
ILoggerFactory logger,
32+
ILoggerFactory loggerFactory,
3133
UrlEncoder encoder,
3234
ISystemClock clock,
3335
ApiKeyProvider apiKeyProvider,
3436
UserCache userCache
35-
) : base(options, logger, encoder, clock)
37+
) : base(options, loggerFactory, encoder, clock)
3638
{
39+
_logger = logger;
3740
_apiKeyProvider = apiKeyProvider;
3841
_userCache = userCache;
3942
}
4043

4144
protected override async Task<AuthenticateResult> HandleAuthenticateAsync() =>
42-
await JsonExceptionMiddleware.InvokeWithExceptionHandler(Request.HttpContext, async () =>
45+
await JsonExceptionMiddleware.InvokeWithExceptionHandler(_logger, Request.HttpContext, async () =>
4346
{
4447
// For endpoints with IAllowAnonymous, skip the authentication check.
4548
if (Context.GetEndpoint()?.Metadata.GetMetadata<IAllowAnonymous>() != null)
@@ -85,4 +88,4 @@ await Response.WriteAsJsonAsync(new
8588
});
8689
}
8790
}
88-
}
91+
}

WebApi/Middleware/JsonExceptionMiddleware.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ internal record CapturedException(
2121

2222
public class JsonExceptionMiddleware
2323
{
24+
private readonly ILogger<JsonExceptionMiddleware> _logger;
2425
private readonly RequestDelegate _next;
2526

26-
public JsonExceptionMiddleware(RequestDelegate next)
27+
public JsonExceptionMiddleware(
28+
ILogger<JsonExceptionMiddleware> logger,
29+
RequestDelegate next
30+
)
2731
{
32+
_logger = logger;
2833
_next = next;
2934
}
3035

@@ -36,25 +41,27 @@ public async Task InvokeAsync(HttpContext httpContext)
3641
}
3742
catch (Exception ex)
3843
{
39-
await HandleExceptionAsync(httpContext, ex);
44+
await HandleExceptionAsync(_logger, httpContext, ex);
4045
}
4146
}
4247

43-
public static async Task<T> InvokeWithExceptionHandler<T>(HttpContext httpContext, Func<Task<T>> action)
48+
public static async Task<T> InvokeWithExceptionHandler<T>(ILogger logger, HttpContext httpContext, Func<Task<T>> action)
4449
{
4550
try
4651
{
4752
return await action();
4853
}
4954
catch (Exception ex)
5055
{
51-
await HandleExceptionAsync(httpContext, ex);
56+
await HandleExceptionAsync(logger, httpContext, ex);
5257
throw;
5358
}
5459
}
5560

56-
private static async Task HandleExceptionAsync(HttpContext httpContext, Exception ex)
61+
private static async Task HandleExceptionAsync(ILogger logger, HttpContext httpContext, Exception ex)
5762
{
63+
logger.LogError(ex, "An unhandled exception occurred.");
64+
5865
HttpStatusCode GetStatusCode()
5966
{
6067
if (ex.Data.Contains("HttpStatusCode") && ex.Data["HttpStatusCode"] is HttpStatusCode statusCode)
@@ -83,4 +90,4 @@ public static class JsonExceptionMiddlewareExtensions
8390
{
8491
public static IApplicationBuilder UseJsonExceptionHandler(this IApplicationBuilder builder) =>
8592
builder.UseMiddleware<JsonExceptionMiddleware>();
86-
}
93+
}

0 commit comments

Comments
 (0)