From a2e7e8841672a49a5d1c4b96754e07c7d75625c9 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Thu, 13 Jun 2019 23:38:39 +0200 Subject: [PATCH 1/3] Renamed RequestAccessor to HttpContextExtensions --- .../{RequestAccessorExtension.cs => HttpContextExtensions.cs} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/Shared/Internal/{RequestAccessorExtension.cs => HttpContextExtensions.cs} (96%) diff --git a/src/Shared/Internal/RequestAccessorExtension.cs b/src/Shared/Internal/HttpContextExtensions.cs similarity index 96% rename from src/Shared/Internal/RequestAccessorExtension.cs rename to src/Shared/Internal/HttpContextExtensions.cs index 24d95e39..5747faa4 100644 --- a/src/Shared/Internal/RequestAccessorExtension.cs +++ b/src/Shared/Internal/HttpContextExtensions.cs @@ -10,7 +10,7 @@ namespace NLog.Web.Internal { - internal static class RequestAccessor + internal static class HttpContextExtensions { #if !ASP_NET_CORE internal static HttpRequestBase TryGetRequest(this HttpContextBase context) From 42e61f6666a8f3121a551f9096746f99c0a48e49 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Thu, 13 Jun 2019 23:39:02 +0200 Subject: [PATCH 2/3] aspnet-session - Reduce log-level to debug when session is not available --- src/Shared/Internal/HttpContextExtensions.cs | 43 ++++++++++++++++--- .../AspNetSessionIdLayoutRenderer.cs | 12 +++--- .../AspNetSessionValueLayoutRenderer.cs | 27 +++--------- 3 files changed, 47 insertions(+), 35 deletions(-) diff --git a/src/Shared/Internal/HttpContextExtensions.cs b/src/Shared/Internal/HttpContextExtensions.cs index 5747faa4..f7b7a8d0 100644 --- a/src/Shared/Internal/HttpContextExtensions.cs +++ b/src/Shared/Internal/HttpContextExtensions.cs @@ -1,12 +1,11 @@ using System; -using System.Text; #if !ASP_NET_CORE using System.Web; -using NLog.Common; #else +using System.Text; using Microsoft.AspNetCore.Http; - #endif +using NLog.Common; namespace NLog.Web.Internal { @@ -17,18 +16,24 @@ internal static HttpRequestBase TryGetRequest(this HttpContextBase context) { try { - return context.Request; + var request = context?.Request; + if (request == null) + InternalLogger.Debug("HttpContext Request Lookup returned null"); + return request; } catch (HttpException ex) { - InternalLogger.Debug(ex, "Exception thrown when accessing Request: " + ex.Message); + InternalLogger.Debug(ex, "HttpContext Request Lookup failed."); return null; } } #else internal static HttpRequest TryGetRequest(this HttpContext context) { - return context.Request; + var request = context?.Request; + if (request == null) + InternalLogger.Debug("HttpContext Request Lookup returned null"); + return request; } #endif @@ -53,5 +58,31 @@ internal static string GetString(this ISession session, string key) return Encoding.UTF8.GetString(data); } #endif + +#if !ASP_NET_CORE + internal static HttpSessionStateBase TryGetSession(this HttpContextBase context) + { + var session = context?.Session; + if (session == null) + InternalLogger.Debug("HttpContext Session Lookup returned null"); + return session; + } +#else + internal static ISession TryGetSession(this HttpContext context) + { + try + { + var session = context?.Session; + if (session == null) + InternalLogger.Debug("HttpContext Session Lookup returned null"); + return session; + } + catch (InvalidOperationException ex) + { + InternalLogger.Debug(ex, "HttpContext Session Lookup failed."); + return null; // System.InvalidOperationException: Session has not been configured for this application or request. + } + } +#endif } } \ No newline at end of file diff --git a/src/Shared/LayoutRenderers/AspNetSessionIdLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetSessionIdLayoutRenderer.cs index 1e24a876..69f386ab 100644 --- a/src/Shared/LayoutRenderers/AspNetSessionIdLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetSessionIdLayoutRenderer.cs @@ -1,7 +1,7 @@ using System.Text; -using NLog.Common; using NLog.Config; using NLog.LayoutRenderers; +using NLog.Web.Internal; #if !ASP_NET_CORE using System.Web; @@ -24,16 +24,14 @@ public class AspNetSessionIdLayoutRenderer : AspNetLayoutRendererBase protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { var context = HttpContextAccessor.HttpContext; - if (context?.Session == null) - { - InternalLogger.Debug("aspnet-sessionid - HttpContext Session is null"); + var contextSession = context.TryGetSession(); + if (contextSession == null) return; - } #if !ASP_NET_CORE - builder.Append(context.Session.SessionID); + builder.Append(contextSession.SessionID); #else - builder.Append(context.Session.Id); + builder.Append(contextSession.Id); #endif } } diff --git a/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs index 052f58c2..e89a9b0c 100644 --- a/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs @@ -1,16 +1,11 @@ using System; using System.Globalization; -using System.Linq; using System.Text; -using NLog.Common; using NLog.Config; using NLog.LayoutRenderers; using NLog.Web.Internal; #if !ASP_NET_CORE using System.Web; -#else -using Microsoft.AspNetCore.Http.Features; - #endif namespace NLog.Web.LayoutRenderers @@ -88,23 +83,16 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) } var context = HttpContextAccessor.HttpContext; - if (context?.Session == null) - { - InternalLogger.Trace("aspnet-session - HttpContext Session is null"); + var contextSession = context.TryGetSession(); + if (contextSession == null) return; - } #if !ASP_NET_CORE - var value = PropertyReader.GetValue(Variable, context.Session, (session,key) => session.Count > 0 ? session[key] : null, EvaluateAsNestedProperties); + var value = PropertyReader.GetValue(Variable, contextSession, (session,key) => session.Count > 0 ? session[key] : null, EvaluateAsNestedProperties); #else //because session.get / session.getstring also creating log messages in some cases, this could lead to stackoverflow issues. //We remember on the context.Items that we are looking up a session value so we prevent stackoverflows - if (context.Items == null || context.Features.Get()?.Session == null) - { - return; - } - - if (context.Items.Count > 0 && context.Items.ContainsKey(NLogRetrievingSessionValue)) + if (context.Items == null || (context.Items.Count > 0 && context.Items.ContainsKey(NLogRetrievingSessionValue))) { return; } @@ -114,12 +102,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) object value; try { - value = PropertyReader.GetValue(Variable, context.Session, (session, key) => session.GetString(key), EvaluateAsNestedProperties); - } - catch (Exception ex) - { - InternalLogger.Warn(ex, "aspnet-session - Retrieving session value failed."); - return; + value = PropertyReader.GetValue(Variable, contextSession, (session, key) => session.GetString(key), EvaluateAsNestedProperties); } finally { From 2d10dd4dffb50a54ab4c4311a3a284379848976c Mon Sep 17 00:00:00 2001 From: Defee Date: Mon, 17 Jun 2019 07:19:35 +0200 Subject: [PATCH 3/3] aspnet-session - Reduce log-level to debug when session is not available (also check for feature) --- src/Shared/Internal/HttpContextExtensions.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Shared/Internal/HttpContextExtensions.cs b/src/Shared/Internal/HttpContextExtensions.cs index f7b7a8d0..1d2b95ff 100644 --- a/src/Shared/Internal/HttpContextExtensions.cs +++ b/src/Shared/Internal/HttpContextExtensions.cs @@ -4,6 +4,7 @@ #else using System.Text; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; #endif using NLog.Common; @@ -72,10 +73,18 @@ internal static ISession TryGetSession(this HttpContext context) { try { - var session = context?.Session; - if (session == null) - InternalLogger.Debug("HttpContext Session Lookup returned null"); - return session; + if (context?.Features.Get()?.Session != null) + { + var session = context?.Session; + if (session == null) + InternalLogger.Debug("HttpContext Session Lookup returned null"); + return session; + } + else + { + InternalLogger.Debug("HttpContext Session Feature not available"); + return null; + } } catch (InvalidOperationException ex) {