diff --git a/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetUserClaimLayoutRenderer.cs b/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetUserClaimLayoutRenderer.cs index eeda7f46..60b0abd0 100644 --- a/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetUserClaimLayoutRenderer.cs +++ b/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetUserClaimLayoutRenderer.cs @@ -64,9 +64,9 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) builder.Append(claim.Value); } } - catch (ObjectDisposedException) + catch (ObjectDisposedException ex) { - //ignore ObjectDisposedException, see https://github.com/NLog/NLog.Web/issues/83 + InternalLogger.Debug(ex, "aspnet-user-claim - HttpContext has been disposed"); } } } diff --git a/src/Shared/Internal/HttpContextExtensions.cs b/src/Shared/Internal/HttpContextExtensions.cs index b55780da..250cec92 100644 --- a/src/Shared/Internal/HttpContextExtensions.cs +++ b/src/Shared/Internal/HttpContextExtensions.cs @@ -124,6 +124,11 @@ internal static ISession TryGetSession(this HttpContext context) return null; } } + catch (ObjectDisposedException ex) + { + InternalLogger.Debug(ex, "HttpContext Session Disposed."); + return null; // System.ObjectDisposedException: IFeatureCollection has been disposed. + } catch (InvalidOperationException ex) { InternalLogger.Debug(ex, "HttpContext Session Lookup failed."); diff --git a/src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs b/src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs index 1c0b6bcb..65d14c05 100644 --- a/src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs +++ b/src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs @@ -14,8 +14,6 @@ using System.Web; #endif - - namespace NLog.Web.LayoutRenderers { /// @@ -72,9 +70,6 @@ private static IHttpContextAccessor RetrieveHttpContextAccessor() return null; } } - - - #endif /// diff --git a/src/Shared/LayoutRenderers/AspNetRequestUrlRenderer.cs b/src/Shared/LayoutRenderers/AspNetRequestUrlRenderer.cs index 6c60aeca..ea4cdcb6 100644 --- a/src/Shared/LayoutRenderers/AspNetRequestUrlRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetRequestUrlRenderer.cs @@ -3,10 +3,10 @@ using NLog.Config; using NLog.LayoutRenderers; using NLog.Web.Internal; +using NLog.Common; #if ASP_NET_CORE using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; - #else using System.Collections.Specialized; using System.Web; @@ -75,7 +75,14 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) return; } - RenderUrl(httpRequest, builder); + try + { + RenderUrl(httpRequest, builder); + } + catch (ObjectDisposedException ex) + { + InternalLogger.Debug(ex, "aspnet-request-url - HttpContext has been disposed"); + } } #if !ASP_NET_CORE diff --git a/src/Shared/LayoutRenderers/AspNetUserAuthTypeLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetUserAuthTypeLayoutRenderer.cs index 3fb4ece2..4c1b1f30 100644 --- a/src/Shared/LayoutRenderers/AspNetUserAuthTypeLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetUserAuthTypeLayoutRenderer.cs @@ -35,9 +35,9 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) builder.Append(identity.AuthenticationType); } - catch (ObjectDisposedException) + catch (ObjectDisposedException ex) { - //ignore ObjectDisposedException, see https://github.com/NLog/NLog.Web/issues/83 + InternalLogger.Debug(ex, "aspnet-user-authtype - HttpContext has been disposed"); } } } diff --git a/src/Shared/LayoutRenderers/AspNetUserIdentityLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetUserIdentityLayoutRenderer.cs index 7264468f..6fe3a9ea 100644 --- a/src/Shared/LayoutRenderers/AspNetUserIdentityLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetUserIdentityLayoutRenderer.cs @@ -35,9 +35,9 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) builder.Append(identity.Name); } - catch (ObjectDisposedException) + catch (ObjectDisposedException ex) { - //ignore ObjectDisposedException, see https://github.com/NLog/NLog.Web/issues/83 + InternalLogger.Debug(ex, "aspnet-user-identity - HttpContext has been disposed"); } } } diff --git a/src/Shared/LayoutRenderers/AspNetUserIsAuthenticatedLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetUserIsAuthenticatedLayoutRenderer.cs index 3bc53a26..1dd73e13 100644 --- a/src/Shared/LayoutRenderers/AspNetUserIsAuthenticatedLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetUserIsAuthenticatedLayoutRenderer.cs @@ -1,5 +1,6 @@ using System; using System.Text; +using NLog.Common; using NLog.Config; using NLog.LayoutRenderers; using NLog.Web.LayoutRenderers; @@ -34,9 +35,9 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) builder.Append(0); } } - catch (ObjectDisposedException) + catch (ObjectDisposedException ex) { - //ignore ObjectDisposedException, see https://github.com/NLog/NLog.Web/issues/83 + InternalLogger.Debug(ex, "aspnet-user-isAuthenticated - HttpContext has been disposed"); } } }