diff --git a/src/Shared/Internal/HttpContextExtensions.cs b/src/Shared/Internal/HttpContextExtensions.cs new file mode 100644 index 00000000..1d2b95ff --- /dev/null +++ b/src/Shared/Internal/HttpContextExtensions.cs @@ -0,0 +1,97 @@ +using System; +#if !ASP_NET_CORE +using System.Web; +#else +using System.Text; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; +#endif +using NLog.Common; + +namespace NLog.Web.Internal +{ + internal static class HttpContextExtensions + { +#if !ASP_NET_CORE + internal static HttpRequestBase TryGetRequest(this HttpContextBase context) + { + try + { + var request = context?.Request; + if (request == null) + InternalLogger.Debug("HttpContext Request Lookup returned null"); + return request; + } + catch (HttpException ex) + { + InternalLogger.Debug(ex, "HttpContext Request Lookup failed."); + return null; + } + } +#else + internal static HttpRequest TryGetRequest(this HttpContext context) + { + var request = context?.Request; + if (request == null) + InternalLogger.Debug("HttpContext Request Lookup returned null"); + return request; + } +#endif + +#if ASP_NET_CORE + internal static string GetString(this ISession session, string key) + { + if (!session.TryGetValue(key, out var data)) + { + return null; + } + + if (data == null) + { + return null; + } + + if (data.Length == 0) + { + return string.Empty; + } + + 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 + { + 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) + { + 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/Internal/RequestAccessorExtension.cs b/src/Shared/Internal/RequestAccessorExtension.cs deleted file mode 100644 index 24d95e39..00000000 --- a/src/Shared/Internal/RequestAccessorExtension.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Text; -#if !ASP_NET_CORE -using System.Web; -using NLog.Common; -#else -using Microsoft.AspNetCore.Http; - -#endif - -namespace NLog.Web.Internal -{ - internal static class RequestAccessor - { -#if !ASP_NET_CORE - internal static HttpRequestBase TryGetRequest(this HttpContextBase context) - { - try - { - return context.Request; - } - catch (HttpException ex) - { - InternalLogger.Debug(ex, "Exception thrown when accessing Request: " + ex.Message); - return null; - } - } -#else - internal static HttpRequest TryGetRequest(this HttpContext context) - { - return context.Request; - } -#endif - -#if ASP_NET_CORE - internal static string GetString(this ISession session, string key) - { - if (!session.TryGetValue(key, out var data)) - { - return null; - } - - if (data == null) - { - return null; - } - - if (data.Length == 0) - { - return string.Empty; - } - - return Encoding.UTF8.GetString(data); - } -#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 {