Skip to content

Commit

Permalink
Merge pull request #87 from NLog/renames-for-nlogweb4.3
Browse files Browse the repository at this point in the history
Renames #2, allow aspnet-request-host in non-core ASP.NET
  • Loading branch information
304NotModified authored Jan 15, 2017
2 parents 330e553 + 1056fa8 commit 92b0e56
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void NullKeyRendersEmptyString()
{
var httpContext = Substitute.For<HttpContextBase>();

var renderer = new AspNetCookieLayoutRenderer();
var renderer = new AspNetRequestCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = null;

Expand All @@ -53,10 +53,10 @@ public void KeyNotFoundRendersEmptyString_Flat_Formatting()
{
var httpContext = Substitute.For<HttpContextBase>();
httpContext.Request.Cookies.Returns(new HttpCookieCollection { new HttpCookie("key1", "TEST") });
var renderer = new AspNetCookieLayoutRenderer();
var renderer = new AspNetRequestCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Flat;
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Flat;

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

Expand All @@ -68,10 +68,10 @@ public void KeyNotFoundRendersEmptyString_Json_Formatting()
{
var httpContext = Substitute.For<HttpContextBase>();
httpContext.Request.Cookies.Returns(new HttpCookieCollection { new HttpCookie("key1", "TEST") });
var renderer = new AspNetCookieLayoutRenderer();
var renderer = new AspNetRequestCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Json;
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;

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

Expand All @@ -89,7 +89,7 @@ public void KeyFoundRendersValue_Cookie_Mulitple_Items_Flat_Formatting()
cookies.Add(cookie);
httpContext.Request.Cookies.Returns(cookies);

var renderer = new AspNetCookieLayoutRenderer();
var renderer = new AspNetRequestCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key" };

Expand All @@ -108,7 +108,7 @@ public void KeyFoundRendersValue_Single_Item_Flat_Formatting()
cookies.Add(cookie);
httpContext.Request.Cookies.Returns(cookies);

var renderer = new AspNetCookieLayoutRenderer();
var renderer = new AspNetRequestCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key" };

Expand All @@ -128,10 +128,10 @@ public void KeyFoundRendersValue_Cookie_Mulitple_Items_Json_Formatting()
cookies.Add(cookie);
httpContext.Request.Cookies.Returns(cookies);

var renderer = new AspNetCookieLayoutRenderer();
var renderer = new AspNetRequestCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Json;
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;

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

Expand All @@ -154,7 +154,7 @@ public void KeyFoundRendersVakue_Cookie_Mulitple_Cookies_Cookie_Items_Flat_Forma

httpContext.Request.Cookies.Returns(cookies);

var renderer = new AspNetCookieLayoutRenderer();
var renderer = new AspNetRequestCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key", "key2" };

Expand All @@ -179,10 +179,10 @@ public void KeyFoundRendersVakue_Cookie_Mulitple_Cookies_Cookie_Items_Json_Forma

httpContext.Request.Cookies.Returns(cookies);

var renderer = new AspNetCookieLayoutRenderer();
var renderer = new AspNetRequestCookieLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.CookieNames = new List<string> { "key", "key2" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Json;
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;

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

Expand Down Expand Up @@ -210,7 +210,7 @@ public void CommaSeperatedCookieNamesTest_Mulitple_FLAT_Formatting()

this.HttpContext.Request.Cookies.Add(cookie);
var t = (DebugTarget)LogManager.Configuration.AllTargets[0];
var renderer = ((SimpleLayout)t.Layout).Renderers[0] as AspNetCookieLayoutRenderer;
var renderer = ((SimpleLayout)t.Layout).Renderers[0] as AspNetRequestCookieLayoutRenderer;

var result = renderer.Render(LogEventInfo.CreateNullEvent());

Expand All @@ -235,7 +235,7 @@ public void CommaSeperatedCookieNamesTest_Mulitple_Json_Formatting()

this.HttpContext.Request.Cookies.Add(cookie);
var t = (DebugTarget)LogManager.Configuration.AllTargets[0];
var renderer = ((SimpleLayout)t.Layout).Renderers[0] as AspNetCookieLayoutRenderer;
var renderer = ((SimpleLayout)t.Layout).Renderers[0] as AspNetRequestCookieLayoutRenderer;

var result = renderer.Render(LogEventInfo.CreateNullEvent());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void KeyNotFoundRendersEmptyString_Flat_Formatting()
var renderer = new AspNetQueryStringLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.QueryStringKeys = new List<string> { "key" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Flat;
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Flat;

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

Expand All @@ -113,7 +113,7 @@ public void KeyNotFoundRendersEmptyString_Json_Formatting()
var renderer = new AspNetQueryStringLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.QueryStringKeys = new List<string> { "key" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Json;
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;

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

Expand All @@ -132,7 +132,7 @@ public void KeyFoundRendersValue_QueryString_Single_Item_Flat_Formatting()
var renderer = new AspNetQueryStringLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.QueryStringKeys = new List<string> { "Id" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Flat;
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Flat;

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

Expand All @@ -151,7 +151,7 @@ public void KeyFoundRendersValue_QueryString_Single_Item_Json_Formatting()
var renderer = new AspNetQueryStringLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.QueryStringKeys = new List<string> { "Id" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Json;
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;

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

Expand All @@ -171,7 +171,7 @@ public void KeyFoundRendersValue_QueryString_Multiple_Item_Flat_Formatting()
var renderer = new AspNetQueryStringLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.QueryStringKeys = new List<string> { "Id", "Id2" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Flat;
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Flat;

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

Expand All @@ -191,7 +191,7 @@ public void KeyFoundRendersValue_QueryString_Multiple_Item_Json_Formatting()
var renderer = new AspNetQueryStringLayoutRenderer();
renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext);
renderer.QueryStringKeys = new List<string> { "Id", "Id2" };
renderer.OutputFormat = AspNetLayoutOutputFormat.Json;
renderer.OutputFormat = AspNetRequestLayoutOutputFormat.Json;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace NLog.Web.Enums
/// <summary>
/// To control the Cookie Renderer Output formatting.
/// </summary>
public enum AspNetLayoutOutputFormat
public enum AspNetRequestLayoutOutputFormat
{
/// <summary>
/// Use this format for rendering the cookie renderer output value as a flat string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace NLog.Web.LayoutRenderers
/// </code>
/// </example>
[LayoutRenderer("aspnet-request-cookie")]
public class AspNetCookieLayoutRenderer : AspNetLayoutRendererBase
public class AspNetRequestCookieLayoutRenderer : AspNetLayoutRendererBase
{
private const string flatCookiesSeparator = "=";
private const string flatItemSeperator = ",";
Expand All @@ -39,7 +39,7 @@ public class AspNetCookieLayoutRenderer : AspNetLayoutRendererBase
/// Determines how the output is rendered. Possible Value: FLAT, JSON. Default is FLAT.
/// </summary>
[DefaultParameter]
public AspNetLayoutOutputFormat OutputFormat { get; set; } = AspNetLayoutOutputFormat.Flat;
public AspNetRequestLayoutOutputFormat OutputFormat { get; set; } = AspNetRequestLayoutOutputFormat.Flat;

/// <summary>
/// Renders the ASP.NET Cookie appends it to the specified <see cref="StringBuilder" />.
Expand Down Expand Up @@ -88,12 +88,12 @@ private void SerializeCookie(string cookieRaw, StringBuilder builder, bool first
{
switch (this.OutputFormat)
{
case AspNetLayoutOutputFormat.Flat:
case AspNetRequestLayoutOutputFormat.Flat:
if (!firstItem)
builder.Append($"{flatItemSeperator}");
builder.Append(cookieRaw);
break;
case AspNetLayoutOutputFormat.Json:
case AspNetRequestLayoutOutputFormat.Json:
if (!firstItem)
builder.Append($"{GlobalConstants.jsonElementSeparator}");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#if NETSTANDARD_1plus

using NLog.LayoutRenderers;
using System.Text;
#if NETSTANDARD_1plus
using Microsoft.AspNetCore.Routing;
#endif
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
{
/// <summary>
/// ASP.NET CORE host.
/// ASP.NET host.
/// </summary>
/// <remarks>
/// Use this layout renderer host.
Expand All @@ -17,8 +19,8 @@ namespace NLog.Web.LayoutRenderers
/// ${aspnet-host}
/// </code>
/// </example>
[LayoutRenderer("aspnet-host")]
public class AspNetCoreHostLayoutRenderer : AspNetLayoutRendererBase
[LayoutRenderer("aspnet-request-host")]
public class AspNetRequestHostLayoutRenderer : AspNetLayoutRendererBase
{
/// <summary>
/// Renders the specified ASP.NET Application variable and appends it to the specified <see cref="StringBuilder" />.
Expand All @@ -27,7 +29,12 @@ public class AspNetCoreHostLayoutRenderer : AspNetLayoutRendererBase
/// <param name="logEvent">Logging event.</param>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var host = HttpContextAccessor?.HttpContext?.TryGetRequest()?.Host;
var request = HttpContextAccessor?.HttpContext?.TryGetRequest();
#if NETSTANDARD_1plus
var host = request?.Host;
#else
var host = request?.UserHostName;
#endif

if (host != null)
{
Expand All @@ -39,4 +46,3 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AspNetQueryStringLayoutRenderer : AspNetLayoutRendererBase
/// Determines how the output is rendered. Possible Value: FLAT, JSON. Default is FLAT.
/// </summary>
[DefaultParameter]
public AspNetLayoutOutputFormat OutputFormat { get; set; } = AspNetLayoutOutputFormat.Flat;
public AspNetRequestLayoutOutputFormat OutputFormat { get; set; } = AspNetRequestLayoutOutputFormat.Flat;

/// <summary>
/// Renders the specified ASP.NET Application variable and appends it to the specified <see cref="StringBuilder" />.
Expand Down Expand Up @@ -98,10 +98,10 @@ private void AppendKeyAndValue(StringBuilder builder, string configuredKey, stri

switch (this.OutputFormat)
{
case AspNetLayoutOutputFormat.Flat:
case AspNetRequestLayoutOutputFormat.Flat:
builder.Append($"{configuredKey}:{value}");
break;
case AspNetLayoutOutputFormat.Json:
case AspNetRequestLayoutOutputFormat.Json:
if (!includeArrayEndBraces)
{
builder.Append(GlobalConstants.jsonArrayStartBraces);
Expand Down
3 changes: 2 additions & 1 deletion NLog.Web/NLog.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetRequestReferrerRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetRequestUrlRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetRequestUserAgent.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetCookieLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetRequestCookieLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetRequestHostLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetRequestQueryStringLayoutRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\LayoutRenderers\AspNetRequestHttpMethodRenderer.cs" />
<Compile Include="..\NLog.Web.AspNetCore\Enums\AspNetCookieLayoutOutputFormat.cs" />
Expand Down

0 comments on commit 92b0e56

Please sign in to comment.