Skip to content

Commit

Permalink
AspNetRequestCookieLayoutRenderer - Added support for Exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Sep 18, 2019
1 parent 5d218aa commit b226c82
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
58 changes: 34 additions & 24 deletions src/Shared/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace NLog.Web.LayoutRenderers
/// <code lang="NLog Layout Renderer">
/// ${aspnet-request-cookie:OutputFormat=Flat}
/// ${aspnet-request-cookie:OutputFormat=Json}
/// ${aspnet-request-cookie:OutputFormat=Json:CookieNames=username}
/// ${aspnet-request-cookie:OutputFormat=Json:Exclude=access_token}
/// </code>
/// </example>
[LayoutRenderer("aspnet-request-cookie")]
Expand All @@ -37,6 +39,24 @@ public class AspNetRequestCookieLayoutRenderer : AspNetLayoutMultiValueRendererB
/// </summary>
public List<string> CookieNames { get; set; }

/// <summary>
/// Gets or sets the keys to exclude from the output. If omitted, none are excluded.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
#if ASP_NET_CORE
public ISet<string> Exclude { get; set; }
#else
public HashSet<string> Exclude { get; set; }
#endif

/// <summary>
/// Initializes a new instance of the <see cref="AspNetRequestCookieLayoutRenderer" /> class.
/// </summary>
public AspNetRequestCookieLayoutRenderer()
{
Exclude = new HashSet<string>(new[] { "AUTH", "SESS_ID" }, StringComparer.OrdinalIgnoreCase);
}

/// <summary>
/// Renders the ASP.NET Cookie appends it to the specified <see cref="StringBuilder" />.
/// </summary>
Expand All @@ -51,38 +71,23 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}

var cookies = httpRequest.Cookies;
var cookieNames = GetCookieNames(cookies)?.ToList();
if (cookieNames?.Count > 0 && cookies?.Count > 0)
if (cookies?.Count > 0)
{
var cookieValues = GetCookies(cookies, cookieNames);
var cookieValues = GetCookieKeyValue(cookies);
SerializePairs(cookieValues, builder, logEvent);
}
}

/// <summary>
/// Get cookies names to render
/// </summary>
/// <param name="cookies"></param>
/// <returns></returns>
private IEnumerable<string> GetCookieNames(Cookies cookies)
{
if (CookieNames != null && CookieNames.Any())
return CookieNames;

var keys = cookies.Keys;

#if !ASP_NET_CORE
return keys.Cast<string>();
#else
return keys;
#endif
}

#if !ASP_NET_CORE
private IEnumerable<KeyValuePair<string, string>> GetCookies(HttpCookieCollection cookies, IEnumerable<string> cookieNames)
private IEnumerable<KeyValuePair<string, string>> GetCookieKeyValue(HttpCookieCollection cookies)
{
var cookieNames = CookieNames?.Count > 0 ? CookieNames : cookies.Keys.Cast<string>().ToList();
bool excludeKeys = (CookieNames == null || cookieNames.Count == 0) && Exclude?.Count > 0;
foreach (var cookieName in cookieNames)
{
if (excludeKeys && Exclude.Contains(cookieName))
continue;

var httpCookie = cookies[cookieName];
if (httpCookie == null)
{
Expand Down Expand Up @@ -111,10 +116,15 @@ private IEnumerable<KeyValuePair<string, string>> GetCookies(HttpCookieCollectio
}
}
#else
private IEnumerable<KeyValuePair<string, string>> GetCookies(IRequestCookieCollection cookies, IEnumerable<string> cookieNames)
private IEnumerable<KeyValuePair<string, string>> GetCookieKeyValue(IRequestCookieCollection cookies)
{
var cookieNames = CookieNames?.Count > 0 ? CookieNames : cookies.Keys;
bool excludeKeys = (CookieNames == null || cookieNames.Count == 0) && Exclude?.Count > 0;
foreach (var cookieName in cookieNames)
{
if (excludeKeys && Exclude.Contains(cookieName))
continue;

if (!cookies.TryGetValue(cookieName, out var cookieValue))
{
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/Shared/LayoutRenderers/AspNetRequestFormLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public class AspNetRequestFormLayoutRenderer : AspNetLayoutMultiValueRendererBas
#endif

/// <summary>
/// Constructor
/// Initializes a new instance of the <see cref="AspNetRequestFormLayoutRenderer" /> class.
/// </summary>
public AspNetRequestFormLayoutRenderer()
{
Include = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
Exclude = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
Exclude = new HashSet<string>(new[] { "Password", "Pwd" }, StringComparer.OrdinalIgnoreCase);
}

/// <summary>
Expand Down
13 changes: 13 additions & 0 deletions tests/Shared/LayoutRenderers/AspNetCookieLayoutRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ public void NullKeyRendersAllCookies()
Assert.Equal(expectedResult, result);
}

[Fact]
public void NullKeyRendersAllCookiesExceptExcluded()
{
var expectedResult = "Key1=TEST1";
var renderer = CreateRenderer();
renderer.CookieNames = null;
renderer.Exclude.Add("key");

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

Assert.Equal(expectedResult, result);
}

[Fact]
public void KeyNotFoundRendersEmptyString_Flat_Formatting()
{
Expand Down

0 comments on commit b226c82

Please sign in to comment.