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

Added ${aspnet-response-statuscode} - rendering response status code #477

Merged
merged 2 commits into from
Sep 24, 2019
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
24 changes: 24 additions & 0 deletions src/Shared/Internal/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ internal static HttpRequestBase TryGetRequest(this HttpContextBase context)
return null;
}
}

internal static HttpResponseBase TryGetResponse(this HttpContextBase context)
{
try
{
var response = context?.Response;
if (response == null)
InternalLogger.Debug("HttpContext Response Lookup returned null");
return response;
}
catch (HttpException ex)
{
InternalLogger.Debug(ex, "HttpContext Response Lookup failed.");
return null;
}
}
#else
internal static HttpRequest TryGetRequest(this HttpContext context)
{
Expand All @@ -36,6 +52,14 @@ internal static HttpRequest TryGetRequest(this HttpContext context)
InternalLogger.Debug("HttpContext Request Lookup returned null");
return request;
}

internal static HttpResponse TryGetResponse(this HttpContext context)
{
var response = context?.Response;
if (response == null)
InternalLogger.Debug("HttpContext Response Lookup returned null");
return response;
}
#endif

#if ASP_NET_CORE
Expand Down
46 changes: 46 additions & 0 deletions src/Shared/LayoutRenderers/AspNetResponseStatusCodeRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Text;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
{
/// <summary>
/// ASP.NET Response Status Code.
/// </summary>
/// <para>Example usage of ${aspnet-response-statuscode}:</para>
/// <example>
/// <code lang="NLog Layout Renderer">
/// ${aspnet-response-statuscode} - Produces - 200.
/// </code>
/// </example>
[LayoutRenderer("aspnet-response-statuscode")]
[ThreadSafe]
public class AspNetResponseStatusCodeRenderer : AspNetLayoutRendererBase
{
/// <summary>
/// ASP.NET Http Response Status Code
/// </summary>
/// <param name="builder">The <see cref="StringBuilder" /> to append the rendered data to.</param>
/// <param name="logEvent">Logging event.</param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpResponse = HttpContextAccessor.HttpContext.TryGetResponse();
if (httpResponse == null)
{
return;
}

int statusCode;
#if !ASP_NET_CORE
statusCode = httpResponse.StatusCode;
#else
statusCode = httpResponse.StatusCode;
304NotModified marked this conversation as resolved.
Show resolved Hide resolved
#endif
if (statusCode >= 100 && statusCode <= 599)
{
builder.Append(statusCode);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using NLog.Web.LayoutRenderers;
using NSubstitute;
using Xunit;

namespace NLog.Web.Tests.LayoutRenderers
{
public class AspNetResponseStatusCodeRendererTests : LayoutRenderersTestBase<AspNetResponseStatusCodeRenderer>
{
[Fact]
public void StatusCode_Set_Renderer()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
httpContext.Response.StatusCode.Returns(200);

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

// Assert
Assert.Equal("200", result);
}

[Theory]
[InlineData(0, false)]
[InlineData(99, false)]
[InlineData(100, true)]
[InlineData(599, true)]
[InlineData(600, false)]
public void Only_Render_Valid_StatusCodes(int statusCode, bool shouldBeRendered)
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();
httpContext.Response.StatusCode.Returns(statusCode);

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

// Assert
if (shouldBeRendered)
{
Assert.Equal($"{statusCode}", result);
}
else
{
Assert.Empty(result);
}
}
}
}