Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpContext - Extended ObjectDisposedException handling #630

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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