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

Suppressing exception thrown when accessing HttpContext.Request #56

Merged
merged 4 commits into from
May 12, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,7 +1,11 @@
using System.Collections.Specialized;
using System.IO;
using System.Web;
using NLog.Common;
using NLog.Config;
using NLog.Web.LayoutRenderers;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Xunit;

namespace NLog.Web.Tests.LayoutRenderers
Expand All @@ -18,6 +22,25 @@ public void NullHttpContextRendersEmptyString()
Assert.Empty(result);
}

[Fact]
public void NullRequestRendersEmptyStringWithoutThrowingException()
{
var internalLog = new StringWriter();
InternalLogger.LogWriter = internalLog;

var httpContext = Substitute.For<HttpContextBase>();
httpContext.Request.Returns(x => { throw new HttpException(); });

var renderer = new AspNetRequestValueLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.Item = "key";

string result = renderer.Render(new LogEventInfo());

Assert.Empty(result);
Assert.Equal(true, string.IsNullOrEmpty(internalLog.ToString()));
}

public class ItemTests
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class AspNetRequestValueLayoutRenderer : AspNetLayoutRendererBase
/// <param name="logEvent">Logging event.</param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpRequest = HttpContextAccessor.HttpContext.Request;
var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest();
if (httpRequest == null)
{
return;
Expand Down Expand Up @@ -137,4 +137,19 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}
}
}

internal static class RequestAccessor
{
internal static HttpRequestBase TryGetRequest(this HttpContextBase context)
{
try
{
return context.Request;
}
catch (HttpException)
{
return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least a InternalLogger.debug(ex) can be added?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compiler refuses to accept InternalLogger.Debug(ex). It will only accept two variants of the Debug method: One taking a string, and the other taking a string and a argument array. Is that as excepted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes that's is added to NLog 4.3.

Debug(Ex.tostring()) is preferred.

Copy link
Contributor Author

@vegar vegar May 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 With or without any further explanation?

InternalLogger.Debug("Exception thrown when accessing Request: " + ex);
or
InternalLogger.Debug(ex.ToString());

Do you want a separate test verifying that it is logged?

Current test passes based on the default log level for the internal logger discarding debug statements. A separate test can be added to verify that the internal logger do pick up on the exception if log level is set appropriately.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first please :)

No need for testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL,

is this a response on "no need for testing"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was taken by surprise. It's not the response I'm used to ;-)

}
}
}
}