Skip to content

Commit

Permalink
Make separators layoutable (${aspnet-request-querystring} and ${aspne…
Browse files Browse the repository at this point in the history
…t-request-cookie}) (#317)

Make ItemSeparator and ValueSeparator layoutable  - ${aspnet-request-querystring} and ${aspnet-request-cookie})
  • Loading branch information
304NotModified authored Sep 22, 2018
1 parent c150c9c commit 9d95873
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ public void KeyFoundRendersValue_Multiple_Cookies_Flat_Formatting_separators()
Assert.Equal(expectedResult, result);
}

[Fact]
public void KeyFoundRendersValue_Multiple_Cookies_Flat_Formatting_separators_layouts()
{
try
{
var expectedResult = "key>TEST" + Environment.NewLine + "Key1>TEST1";
GlobalDiagnosticsContext.Set("valueSeparator1", ">");

var renderer = CreateRenderer();
renderer.ValueSeparator = "${gdc:valueSeparator1}";
renderer.ItemSeparator = "${newline}";

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

Assert.Equal(expectedResult, result);
}
finally
{
//clean up
GlobalDiagnosticsContext.Clear();
}
}

[Fact]
public void KeyFoundRendersValue_Single_Cookie_Flat_Formatting()
{
Expand Down Expand Up @@ -272,7 +295,7 @@ public void KeyFoundRendersValue_Cookie_Multiple_Items_Json_Formatting_ValuesOnl
Assert.Equal(expectedResult, result);
}

//no multivalue cookie keys in ASP.NET core
//no multivalue cookie keys in ASP.NET core
#if !ASP_NET_CORE

[Fact]
Expand Down Expand Up @@ -422,7 +445,7 @@ void AddCookie(string key, string result)
cookies.Add(cookie2);
cookieNames.Add("Key1");
}

if (addMultiValueCookieKey)
{
var multiValueCookie = new HttpCookie("key2", "Test");
Expand Down
5 changes: 5 additions & 0 deletions NLog.Web.AspNetCore.Tests/NLog.Web.AspNetCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Web">
<HintPath>C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Web.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog.Config;
using NLog.Layouts;
using NLog.Web.Enums;

namespace NLog.Web.LayoutRenderers
Expand All @@ -11,15 +13,38 @@ namespace NLog.Web.LayoutRenderers
/// </summary>
public abstract class AspNetLayoutMultiValueRendererBase : AspNetLayoutRendererBase
{
private string _itemSeparator = ",";
private Layout _itemSeparatorLayout = ",";
private string _valueSeparator = "=";
private Layout _valueSeparatorLayout = "=";

/// <summary>
/// Separator between item. Only used for <see cref="AspNetRequestLayoutOutputFormat.Flat"/>
/// </summary>
public string ItemSeparator { get; set; } = ",";
/// <remarks>Render with <see cref="GetRenderedItemSeparator"/></remarks>
public string ItemSeparator
{
get => _itemSeparator;
set
{
_itemSeparator = value;
_itemSeparatorLayout = value;
}
}

/// <summary>
/// Separator between value and key. Only used for <see cref="AspNetRequestLayoutOutputFormat.Flat"/>
/// </summary>
public string ValueSeparator { get; set; } = "=";
/// <remarks>Render with <see cref="GetRenderedValueSeparator"/></remarks>
public string ValueSeparator
{
get => _valueSeparator;
set
{
_valueSeparator = value;
_valueSeparatorLayout = value;
}
}

/// <summary>
/// Single item in array? Only used for <see cref="AspNetRequestLayoutOutputFormat.Json"/>
Expand All @@ -44,12 +69,24 @@ public abstract class AspNetLayoutMultiValueRendererBase : AspNetLayoutRendererB
/// </summary>
/// <param name="pairs">The key/value pairs.</param>
/// <param name="builder">Add to this builder.</param>
[Obsolete("use SerializePairs with logEvent to support Layouts for Separator. This overload will be removed in NLog.Web(aspNetCore) 5")]
protected void SerializePairs(IEnumerable<KeyValuePair<string, string>> pairs, StringBuilder builder)
{
SerializePairs(pairs, builder, null);
}

/// <summary>
/// Serialize multiple key/value pairs
/// </summary>
/// <param name="pairs">The key/value pairs.</param>
/// <param name="builder">Add to this builder.</param>
/// <param name="logEvent">Log event for rendering separators.</param>
protected void SerializePairs(IEnumerable<KeyValuePair<string, string>> pairs, StringBuilder builder, LogEventInfo logEvent)
{
switch (OutputFormat)
{
case AspNetRequestLayoutOutputFormat.Flat:
SerializePairsFlat(pairs, builder);
SerializePairsFlat(pairs, builder, logEvent);
break;
case AspNetRequestLayoutOutputFormat.Json:
SerializePairsJson(pairs, builder);
Expand Down Expand Up @@ -105,8 +142,12 @@ private void SerializePairsJson(IEnumerable<KeyValuePair<string, string>> pairs,
}
}

private void SerializePairsFlat(IEnumerable<KeyValuePair<string, string>> pairs, StringBuilder builder)
private void SerializePairsFlat(IEnumerable<KeyValuePair<string, string>> pairs, StringBuilder builder,
LogEventInfo logEvent)
{
var itemSeparator = GetRenderedItemSeparator(logEvent);
var valueSeparator = GetRenderedValueSeparator(logEvent);

var firstItem = true;
foreach (var kpv in pairs)
{
Expand All @@ -115,20 +156,41 @@ private void SerializePairsFlat(IEnumerable<KeyValuePair<string, string>> pairs,

if (!firstItem)
{
builder.Append(ItemSeparator);
builder.Append(itemSeparator);
}
firstItem = false;

if (!ValuesOnly)
{
builder.Append(key);
builder.Append(ValueSeparator);

builder.Append(valueSeparator);
}

builder.Append(value);
}
}

/// <summary>
/// Get the rendered <see cref="ItemSeparator"/>
/// </summary>
/// <param name="logEvent"></param>
/// <returns></returns>
protected string GetRenderedItemSeparator(LogEventInfo logEvent)
{
return logEvent != null ? _itemSeparatorLayout.Render(logEvent) : ItemSeparator;
}

/// <summary>
/// Get the rendered <see cref="ValueSeparator"/>
/// </summary>
/// <param name="logEvent"></param>
/// <returns></returns>
protected string GetRenderedValueSeparator(LogEventInfo logEvent)
{
return logEvent != null ? _valueSeparatorLayout.Render(logEvent) : ValueSeparator;
}

/// <summary>
/// Append the value quoted, escape quotes when needed
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
if (CookieNames?.Count > 0 && cookies?.Count > 0)
{
var cookieValues = GetCookies(cookies);
SerializePairs(cookieValues, builder);
SerializePairs(cookieValues, builder, logEvent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
#endif

var pairs = GetPairs(queryStrings, queryStringKeys);
SerializePairs(pairs, builder);
SerializePairs(pairs, builder, logEvent);
}

private static IEnumerable<KeyValuePair<string, string>> GetPairs(
Expand Down

0 comments on commit 9d95873

Please sign in to comment.