From 594c12beb1cc4ee4c659b0e860e658ceef4d6f46 Mon Sep 17 00:00:00 2001 From: Vegar Vikan Date: Wed, 11 May 2016 19:56:04 +0200 Subject: [PATCH 1/4] Suppressing exception thrown when accessing HttpContext.Request --- .../AspNetRequestValueLayoutRendererTests.cs | 23 +++++++++++++++++++ .../AspNetRequestValueLayoutRenderer.cs | 17 +++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/NLog.Web.ASPNET5.Tests/LayoutRenderers/AspNetRequestValueLayoutRendererTests.cs b/NLog.Web.ASPNET5.Tests/LayoutRenderers/AspNetRequestValueLayoutRendererTests.cs index 735409c9..58c4c681 100644 --- a/NLog.Web.ASPNET5.Tests/LayoutRenderers/AspNetRequestValueLayoutRendererTests.cs +++ b/NLog.Web.ASPNET5.Tests/LayoutRenderers/AspNetRequestValueLayoutRendererTests.cs @@ -1,7 +1,11 @@ using System.Collections.Specialized; +using System.IO; using System.Web; +using NLog.Common; +using NLog.Config; using NLog.Web.LayoutRenderers; using NSubstitute; +using NSubstitute.ExceptionExtensions; using Xunit; namespace NLog.Web.Tests.LayoutRenderers @@ -18,6 +22,25 @@ public void NullHttpContextRendersEmptyString() Assert.Empty(result); } + [Fact] + public void NullRequestRendersEmptyStringWithoutThrowingException() + { + var internalLog = new StringWriter(); + InternalLogger.LogWriter = internalLog; + + var httpContext = Substitute.For(); + httpContext.Request.Returns(x => { throw new HttpException(); }); + + var renderer = new AspNetRequestValueLayoutRenderer(); + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.Item = "key"; + + string result = renderer.Render(new LogEventInfo()); + + Assert.Empty(result); + Assert.Equal(true, string.IsNullOrEmpty(internalLog.ToString())); + } + public class ItemTests { [Fact] diff --git a/NLog.Web.ASPNET5/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs b/NLog.Web.ASPNET5/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs index 302edff3..8efd9501 100644 --- a/NLog.Web.ASPNET5/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs +++ b/NLog.Web.ASPNET5/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs @@ -76,7 +76,7 @@ public class AspNetRequestValueLayoutRenderer : AspNetLayoutRendererBase /// Logging event. protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { - var httpRequest = HttpContextAccessor.HttpContext.Request; + var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest(); if (httpRequest == null) { return; @@ -137,4 +137,19 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) } } } + + internal static class RequestAccessor + { + internal static HttpRequestBase TryGetRequest(this HttpContextBase context) + { + try + { + return context.Request; + } + catch (HttpException) + { + return null; + } + } + } } \ No newline at end of file From d67fb01905cc60f46a4b439c6453c7ad41285aa6 Mon Sep 17 00:00:00 2001 From: Vegar Vikan Date: Wed, 11 May 2016 21:31:45 +0200 Subject: [PATCH 2/4] Logging suppressed exception to InternalLogger --- .../LayoutRenderers/AspNetRequestValueLayoutRenderer.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/NLog.Web.ASPNET5/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs b/NLog.Web.ASPNET5/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs index 8efd9501..21be8afb 100644 --- a/NLog.Web.ASPNET5/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs +++ b/NLog.Web.ASPNET5/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs @@ -1,5 +1,6 @@ using System; using System.Text; +using NLog.Common; #if !DNX using System.Web; #else @@ -146,8 +147,9 @@ internal static HttpRequestBase TryGetRequest(this HttpContextBase context) { return context.Request; } - catch (HttpException) + catch (HttpException ex) { + InternalLogger.Debug("Exception thrown when accessing Request: " + ex); return null; } } From d26bb54cd27ea90efc14a73059cd83162d5d2c5f Mon Sep 17 00:00:00 2001 From: Vegar Vikan Date: Thu, 12 May 2016 09:45:30 +0200 Subject: [PATCH 3/4] No catch for .net core --- .../LayoutRenderers/AspNetRequestValueLayoutRenderer.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/NLog.Web.ASPNET5/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs b/NLog.Web.ASPNET5/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs index 21be8afb..27bc6ded 100644 --- a/NLog.Web.ASPNET5/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs +++ b/NLog.Web.ASPNET5/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs @@ -141,6 +141,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) internal static class RequestAccessor { +#if !DNX internal static HttpRequestBase TryGetRequest(this HttpContextBase context) { try @@ -153,5 +154,12 @@ internal static HttpRequestBase TryGetRequest(this HttpContextBase context) return null; } } +#else + internal static HttpRequest TryGetRequest(this HttpContext context) + { + return context.Request; + } +#endif } + } \ No newline at end of file From a1e88eaf9c17840548c635adf10bdec932bd0fdf Mon Sep 17 00:00:00 2001 From: Vegar Vikan Date: Thu, 12 May 2016 09:45:56 +0200 Subject: [PATCH 4/4] ensure internal loglevel is set to error for test to pass --- .../LayoutRenderers/AspNetRequestValueLayoutRendererTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NLog.Web.ASPNET5.Tests/LayoutRenderers/AspNetRequestValueLayoutRendererTests.cs b/NLog.Web.ASPNET5.Tests/LayoutRenderers/AspNetRequestValueLayoutRendererTests.cs index 58c4c681..e30f4f0f 100644 --- a/NLog.Web.ASPNET5.Tests/LayoutRenderers/AspNetRequestValueLayoutRendererTests.cs +++ b/NLog.Web.ASPNET5.Tests/LayoutRenderers/AspNetRequestValueLayoutRendererTests.cs @@ -23,10 +23,11 @@ public void NullHttpContextRendersEmptyString() } [Fact] - public void NullRequestRendersEmptyStringWithoutThrowingException() + public void NullRequestRendersEmptyStringWithoutLoggingError() { var internalLog = new StringWriter(); InternalLogger.LogWriter = internalLog; + InternalLogger.LogLevel = LogLevel.Error; var httpContext = Substitute.For(); httpContext.Request.Returns(x => { throw new HttpException(); });