From e44dae2fcbbc6207b680446c30580b138b7b7820 Mon Sep 17 00:00:00 2001 From: Mole Date: Mon, 12 Oct 2020 15:27:07 +0200 Subject: [PATCH] Add first swing at UnhandledExceptionLoggerMiddleware --- .../BackOfficeApplicationBuilderExtensions.cs | 2 + .../Runtime/BackOfficeComposer.cs | 1 + .../UnhandledExceptionLoggerMiddleware.cs | 43 +++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 src/Umbraco.Web.BackOffice/UnhandledExceptionLoggerMiddleware.cs diff --git a/src/Umbraco.Web.BackOffice/Extensions/BackOfficeApplicationBuilderExtensions.cs b/src/Umbraco.Web.BackOffice/Extensions/BackOfficeApplicationBuilderExtensions.cs index 471aed51e1..a64577887c 100644 --- a/src/Umbraco.Web.BackOffice/Extensions/BackOfficeApplicationBuilderExtensions.cs +++ b/src/Umbraco.Web.BackOffice/Extensions/BackOfficeApplicationBuilderExtensions.cs @@ -2,6 +2,7 @@ using System; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using SixLabors.ImageSharp.Web.DependencyInjection; +using Umbraco.Web.BackOffice; using Umbraco.Web.BackOffice.Routing; using Umbraco.Web.BackOffice.Security; @@ -46,6 +47,7 @@ namespace Umbraco.Extensions app.UseUmbracoRuntimeMinification(); app.UseMiddleware(); + app.UseMiddleware(); return app; } diff --git a/src/Umbraco.Web.BackOffice/Runtime/BackOfficeComposer.cs b/src/Umbraco.Web.BackOffice/Runtime/BackOfficeComposer.cs index 1ce0724aea..3edf8c4177 100644 --- a/src/Umbraco.Web.BackOffice/Runtime/BackOfficeComposer.cs +++ b/src/Umbraco.Web.BackOffice/Runtime/BackOfficeComposer.cs @@ -48,6 +48,7 @@ namespace Umbraco.Web.BackOffice.Runtime "~/")); composition.RegisterUnique(); + composition.RegisterUnique(); } } } diff --git a/src/Umbraco.Web.BackOffice/UnhandledExceptionLoggerMiddleware.cs b/src/Umbraco.Web.BackOffice/UnhandledExceptionLoggerMiddleware.cs new file mode 100644 index 0000000000..d131b9cadd --- /dev/null +++ b/src/Umbraco.Web.BackOffice/UnhandledExceptionLoggerMiddleware.cs @@ -0,0 +1,43 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Extensions; +using Microsoft.Extensions.Logging; +using Umbraco.Core; + +namespace Umbraco.Web.BackOffice +{ + public class UnhandledExceptionLoggerMiddleware : IMiddleware + { + private readonly ILogger _logger; + + public UnhandledExceptionLoggerMiddleware(ILogger logger) + { + _logger = logger; + } + + public async Task InvokeAsync(HttpContext context, RequestDelegate next) + { + var requestUri = new Uri(context.Request.GetEncodedUrl(), UriKind.RelativeOrAbsolute); + // If it's a client side request just call next and don't try to log anything + if (requestUri.IsClientSideRequest()) + { + await next(context); + } + else + { + // We call the next middleware, and catch any errors that occurs in the rest of the pipeline + try + { + await next(context); + } + catch (Exception e) + { + _logger.LogError(e, "Unhandled controller exception occurred for request '{RequestUrl}'", requestUri.AbsoluteUri); + // throw the error again, just in case it gets handled (which is shouldn't) + throw; + } + } + } + } +}