From ca0b6c6e6037a2ff39d24c9a7d8e408736c6bd6b Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Thu, 27 Sep 2018 15:26:19 -0400 Subject: [PATCH] Adds logging for cancelled requests and DbUpdateExceptions --- .../Core/ApplicationBuilderExtensions.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Tgstation.Server.Host/Core/ApplicationBuilderExtensions.cs b/src/Tgstation.Server.Host/Core/ApplicationBuilderExtensions.cs index becd3e5ad6..971d018f73 100644 --- a/src/Tgstation.Server.Host/Core/ApplicationBuilderExtensions.cs +++ b/src/Tgstation.Server.Host/Core/ApplicationBuilderExtensions.cs @@ -1,6 +1,9 @@ using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using System; using System.Globalization; using Tgstation.Server.Api.Models; @@ -12,6 +15,13 @@ namespace Tgstation.Server.Host.Core /// static class ApplicationBuilderExtensions { + /// + /// Gets a from a given + /// + /// The to get the from + /// A new + static ILogger GetLogger(HttpContext httpContext) => httpContext.RequestServices.GetRequiredService>(); + /// /// Return a for s /// @@ -22,12 +32,14 @@ namespace Tgstation.Server.Host.Core throw new ArgumentNullException(nameof(applicationBuilder)); applicationBuilder.Use(async (context, next) => { + var logger = GetLogger(context); try { await next().ConfigureAwait(false); } catch (DbUpdateException e) { + logger.LogDebug("Database conflict: {0}", e.Message); await new ConflictObjectResult(new ErrorMessage { Message = String.Format(CultureInfo.InvariantCulture, "A database conflict has occurred: {0}", (e.InnerException ?? e).Message) }).ExecuteResultAsync(new ActionContext { HttpContext = context @@ -46,11 +58,15 @@ namespace Tgstation.Server.Host.Core throw new ArgumentNullException(nameof(applicationBuilder)); applicationBuilder.Use(async (context, next) => { + var logger = GetLogger(context); try { await next().ConfigureAwait(false); } - catch (OperationCanceledException) { } + catch (OperationCanceledException) + { + logger.LogDebug("Request cancelled!"); + } }); } }