From e688077175a758f793112a91a13dd7a37f5a60a9 Mon Sep 17 00:00:00 2001 From: "Eli C. Lowry" <83078660+Enkidu93@users.noreply.github.com> Date: Wed, 30 Aug 2023 10:53:19 -0400 Subject: [PATCH] Fixes #91 --ECL (#102) Fix as per PR --ECL --- .../Controllers/HttpResultFilter.cs | 23 +++++++++++++++++++ .../Controllers/ServalControllerBase.cs | 1 + 2 files changed, 24 insertions(+) create mode 100644 src/Serval.Shared/Controllers/HttpResultFilter.cs diff --git a/src/Serval.Shared/Controllers/HttpResultFilter.cs b/src/Serval.Shared/Controllers/HttpResultFilter.cs new file mode 100644 index 00000000..eb381ae2 --- /dev/null +++ b/src/Serval.Shared/Controllers/HttpResultFilter.cs @@ -0,0 +1,23 @@ +using System.Diagnostics; + +namespace Serval.Shared.Controllers +{ + public class HttpResultFilter : ResultFilterAttribute + { + private readonly ILogger _logger; + + public HttpResultFilter(ILoggerFactory loggerFactory) + { + _logger = loggerFactory.CreateLogger(); + } + + public override Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) + { + if ((context.Result is ObjectResult r) && (r.StatusCode >= 400)) + { + _logger.LogInformation($"Responded with code {r.StatusCode}. Trace: {Activity.Current?.Id}"); + } + return base.OnResultExecutionAsync(context, next); + } + } +} diff --git a/src/Serval.Shared/Controllers/ServalControllerBase.cs b/src/Serval.Shared/Controllers/ServalControllerBase.cs index 38ced2fa..7ca55c75 100644 --- a/src/Serval.Shared/Controllers/ServalControllerBase.cs +++ b/src/Serval.Shared/Controllers/ServalControllerBase.cs @@ -5,6 +5,7 @@ [TypeFilter(typeof(OperationCancelledExceptionFilter))] [TypeFilter(typeof(NotSupportedExceptionFilter))] [TypeFilter(typeof(ServiceUnavailableException))] +[TypeFilter(typeof(HttpResultFilter))] public abstract class ServalControllerBase : Controller { private readonly IAuthorizationService _authService;