Skip to content

Commit

Permalink
HttpContext - Extended ObjectDisposedException handling (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Jan 26, 2021
1 parent 860a838 commit be7c6eb
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Shared/Internal/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
5 changes: 0 additions & 5 deletions src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
using System.Web;
#endif



namespace NLog.Web.LayoutRenderers
{
/// <summary>
Expand Down Expand Up @@ -72,9 +70,6 @@ private static IHttpContextAccessor RetrieveHttpContextAccessor()
return null;
}
}



#endif

/// <summary>
Expand Down
11 changes: 9 additions & 2 deletions src/Shared/LayoutRenderers/AspNetRequestUrlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text;
using NLog.Common;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.LayoutRenderers;
Expand Down Expand Up @@ -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");
}
}
}
Expand Down

0 comments on commit be7c6eb

Please sign in to comment.