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;