Skip to content

Commit

Permalink
AspNetResponseCookieLayoutRenderer - Improved handling of empty prope…
Browse files Browse the repository at this point in the history
…rties with Verbose (#799)
  • Loading branch information
snakefoot authored Jun 10, 2022
1 parent b838155 commit 61f8567
Showing 1 changed file with 60 additions and 62 deletions.
122 changes: 60 additions & 62 deletions src/Shared/LayoutRenderers/AspNetResponseCookieLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,50 +108,42 @@ private string GetRenderedObjectSeparator(LogEventInfo logEvent)
/// <summary>
/// Append the quoted name and value separated by a colon
/// </summary>
/// <param name="builder"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="skipPropertySeparator"></param>
private static void AppendJsonProperty(StringBuilder builder, string name, string value, bool skipPropertySeparator = false)
private static bool AppendJsonProperty(StringBuilder builder, string name, string value, bool includePropertySeparator)
{
if (!string.IsNullOrEmpty(value))
{
AppendQuoted(builder, name);
builder.Append(':');
AppendQuoted(builder, value);
if (!skipPropertySeparator)
if (includePropertySeparator)
{
builder.Append(',');
}
AppendQuoted(builder, name);
builder.Append(':');
AppendQuoted(builder, value);
return true;
}
return false;
}

/// <summary>
/// Append the quoted name and value separated by a value separator
/// and ended by item separator
/// </summary>
/// <param name="builder"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="logEvent"></param>
/// <param name="skipItemSeparator"></param>
private void AppendFlatProperty(
private static bool AppendFlatProperty(
StringBuilder builder,
string name,
string value,
LogEventInfo logEvent,
bool skipItemSeparator = false)
string valueSeparator,
string itemSeparator)
{
if (!string.IsNullOrEmpty(value))
{
builder.Append(itemSeparator);
builder.Append(name);
builder.Append(GetRenderedValueSeparator(logEvent));
builder.Append(valueSeparator);
builder.Append(value);
if (!skipItemSeparator)
{
builder.Append(GetRenderedItemSeparator(logEvent));
}
return true;
}
return false;
}

#if !ASP_NET_CORE
Expand Down Expand Up @@ -195,6 +187,7 @@ private void SerializeAllProperties(IEnumerable<HttpCookie> verboseCookieValues,
private void SerializeAllPropertiesJson(IEnumerable<HttpCookie> verboseCookieValues, StringBuilder builder)
{
var firstItem = true;
var includeSeparator = false;

foreach (var cookie in verboseCookieValues)
{
Expand All @@ -216,13 +209,13 @@ private void SerializeAllPropertiesJson(IEnumerable<HttpCookie> verboseCookieVal

builder.Append('{');

AppendJsonProperty(builder, nameof(cookie.Name), cookie.Name);
AppendJsonProperty(builder, nameof(cookie.Value), cookie.Value);
AppendJsonProperty(builder, nameof(cookie.Domain), cookie.Domain);
AppendJsonProperty(builder, nameof(cookie.Path), cookie.Path);
AppendJsonProperty(builder, nameof(cookie.Expires), cookie.Expires.ToUniversalTime().ToString("u"));
AppendJsonProperty(builder, nameof(cookie.Secure), cookie.Secure.ToString());
AppendJsonProperty(builder, nameof(cookie.HttpOnly), cookie.HttpOnly.ToString(),skipPropertySeparator: true);
includeSeparator = AppendJsonProperty(builder, nameof(cookie.Name), cookie.Name, false);
includeSeparator = AppendJsonProperty(builder, nameof(cookie.Value), cookie.Value, includeSeparator) || includeSeparator;
includeSeparator = AppendJsonProperty(builder, nameof(cookie.Domain), cookie.Domain, includeSeparator) || includeSeparator;
includeSeparator = AppendJsonProperty(builder, nameof(cookie.Path), cookie.Path, includeSeparator) || includeSeparator;
includeSeparator = AppendJsonProperty(builder, nameof(cookie.Expires), cookie.Expires.ToUniversalTime().ToString("u"), includeSeparator) || includeSeparator;
includeSeparator = AppendJsonProperty(builder, nameof(cookie.Secure), cookie.Secure.ToString(), includeSeparator) || includeSeparator;
includeSeparator = AppendJsonProperty(builder, nameof(cookie.HttpOnly), cookie.HttpOnly.ToString(), includeSeparator) || includeSeparator;

builder.Append('}');

Expand All @@ -244,25 +237,27 @@ private void SerializeAllPropertiesJson(IEnumerable<HttpCookie> verboseCookieVal

private void SerializeAllPropertiesFlat(IEnumerable<HttpCookie> verboseCookieValues, StringBuilder builder, LogEventInfo logEvent)
{
var propertySeparator = GetRenderedItemSeparator(logEvent);
var valueSeparator = GetRenderedValueSeparator(logEvent);
var objectSeparator = GetRenderedObjectSeparator(logEvent);

var firstItem = true;
var firstObject = true;
var includeSeparator = false;
foreach (var cookie in verboseCookieValues)
{
if (!firstItem)
if (!firstObject)
{
builder.Append(objectSeparator);
}

firstItem = false;

AppendFlatProperty(builder, nameof(cookie.Name), cookie.Name, logEvent);
AppendFlatProperty(builder, nameof(cookie.Value), cookie.Value, logEvent);
AppendFlatProperty(builder, nameof(cookie.Domain), cookie.Domain, logEvent);
AppendFlatProperty(builder, nameof(cookie.Path), cookie.Path, logEvent);
AppendFlatProperty(builder, nameof(cookie.Expires), cookie.Expires.ToUniversalTime().ToString("u"), logEvent);
AppendFlatProperty(builder, nameof(cookie.Secure), cookie.Secure.ToString(), logEvent);
AppendFlatProperty(builder, nameof(cookie.HttpOnly), cookie.HttpOnly.ToString(), logEvent, skipItemSeparator: true);
firstObject = false;

includeSeparator = AppendFlatProperty(builder, nameof(cookie.Name), cookie.Name, valueSeparator, "");
includeSeparator = AppendFlatProperty(builder, nameof(cookie.Value), cookie.Value, valueSeparator, includeSeparator ? propertySeparator : "") || includeSeparator;
includeSeparator = AppendFlatProperty(builder, nameof(cookie.Domain), cookie.Domain, valueSeparator, includeSeparator ? propertySeparator : "") || includeSeparator;
includeSeparator = AppendFlatProperty(builder, nameof(cookie.Path), cookie.Path, valueSeparator, includeSeparator ? propertySeparator : "") || includeSeparator;
includeSeparator = AppendFlatProperty(builder, nameof(cookie.Expires), cookie.Expires.ToUniversalTime().ToString("u"), valueSeparator, includeSeparator ? propertySeparator : "") || includeSeparator;
includeSeparator = AppendFlatProperty(builder, nameof(cookie.Secure), cookie.Secure.ToString(), valueSeparator, includeSeparator ? propertySeparator : "") || includeSeparator;
includeSeparator = AppendFlatProperty(builder, nameof(cookie.HttpOnly), cookie.HttpOnly.ToString(), valueSeparator, includeSeparator ? propertySeparator : "") || includeSeparator;
}
}

Expand Down Expand Up @@ -382,6 +377,7 @@ private void SerializeAllProperties(IEnumerable<SetCookieHeaderValue> verboseCoo
private void SerializeAllPropertiesJson(IEnumerable<SetCookieHeaderValue> verboseCookieValues, StringBuilder builder)
{
var firstItem = true;
var includeSeparator = false;

foreach (var cookie in verboseCookieValues)
{
Expand All @@ -403,14 +399,14 @@ private void SerializeAllPropertiesJson(IEnumerable<SetCookieHeaderValue> verbos

builder.Append('{');

AppendJsonProperty(builder, nameof(cookie.Name), cookie.Name.ToString());
AppendJsonProperty(builder, nameof(cookie.Value), cookie.Value.ToString());
AppendJsonProperty(builder, nameof(cookie.Domain), cookie.Domain.ToString());
AppendJsonProperty(builder, nameof(cookie.Path), cookie.Path.ToString());
AppendJsonProperty(builder, nameof(cookie.Expires), cookie.Expires?.ToUniversalTime().ToString("u"));
AppendJsonProperty(builder, nameof(cookie.Secure), cookie.Secure.ToString());
AppendJsonProperty(builder, nameof(cookie.HttpOnly), cookie.HttpOnly.ToString());
AppendJsonProperty(builder, nameof(cookie.SameSite), cookie.SameSite.ToString(), skipPropertySeparator: true);
includeSeparator = AppendJsonProperty(builder, nameof(cookie.Name), cookie.Name.ToString(), false);
includeSeparator = AppendJsonProperty(builder, nameof(cookie.Value), cookie.Value.ToString(), includeSeparator) || includeSeparator;
includeSeparator = AppendJsonProperty(builder, nameof(cookie.Domain), cookie.Domain.ToString(), includeSeparator) || includeSeparator;
includeSeparator = AppendJsonProperty(builder, nameof(cookie.Path), cookie.Path.ToString(), includeSeparator) || includeSeparator;
includeSeparator = AppendJsonProperty(builder, nameof(cookie.Expires), cookie.Expires?.ToUniversalTime().ToString("u"), includeSeparator) || includeSeparator;
includeSeparator = AppendJsonProperty(builder, nameof(cookie.Secure), cookie.Secure.ToString(), includeSeparator) || includeSeparator;
includeSeparator = AppendJsonProperty(builder, nameof(cookie.HttpOnly), cookie.HttpOnly.ToString(), includeSeparator) || includeSeparator;
includeSeparator = AppendJsonProperty(builder, nameof(cookie.SameSite), cookie.SameSite.ToString(), includeSeparator) || includeSeparator;

builder.Append('}');

Expand All @@ -432,26 +428,28 @@ private void SerializeAllPropertiesJson(IEnumerable<SetCookieHeaderValue> verbos

private void SerializeAllPropertiesFlat(IEnumerable<SetCookieHeaderValue> verboseCookieValues, StringBuilder builder, LogEventInfo logEvent)
{
var propertySeparator = GetRenderedItemSeparator(logEvent);
var valueSeparator = GetRenderedValueSeparator(logEvent);
var objectSeparator = GetRenderedObjectSeparator(logEvent);

var firstItem = true;
var firstObject = true;
var includeSeparator = false;
foreach (var cookie in verboseCookieValues)
{
if (!firstItem)
if (!firstObject)
{
builder.Append(objectSeparator);
}

firstItem = false;

AppendFlatProperty(builder, nameof(cookie.Name), cookie.Name.ToString(), logEvent);
AppendFlatProperty(builder, nameof(cookie.Value), cookie.Value.ToString(), logEvent);
AppendFlatProperty(builder, nameof(cookie.Domain), cookie.Domain.ToString(), logEvent);
AppendFlatProperty(builder, nameof(cookie.Path), cookie.Path.ToString(), logEvent);
AppendFlatProperty(builder, nameof(cookie.Expires), cookie.Expires?.ToUniversalTime().ToString("u"), logEvent);
AppendFlatProperty(builder, nameof(cookie.Secure), cookie.Secure.ToString(), logEvent);
AppendFlatProperty(builder, nameof(cookie.HttpOnly), cookie.HttpOnly.ToString(), logEvent);
AppendFlatProperty(builder, nameof(cookie.SameSite), cookie.SameSite.ToString(), logEvent, skipItemSeparator: true);
firstObject = false;

includeSeparator = AppendFlatProperty(builder, nameof(cookie.Name), cookie.Name.ToString(), valueSeparator, "");
includeSeparator = AppendFlatProperty(builder, nameof(cookie.Value), cookie.Value.ToString(), valueSeparator, includeSeparator ? propertySeparator : "") || includeSeparator;
includeSeparator = AppendFlatProperty(builder, nameof(cookie.Domain), cookie.Domain.ToString(), valueSeparator, includeSeparator ? propertySeparator : "") || includeSeparator;
includeSeparator = AppendFlatProperty(builder, nameof(cookie.Path), cookie.Path.ToString(), valueSeparator, includeSeparator ? propertySeparator : "") || includeSeparator;
includeSeparator = AppendFlatProperty(builder, nameof(cookie.Expires), cookie.Expires?.ToUniversalTime().ToString("u"), valueSeparator, includeSeparator ? propertySeparator : "") || includeSeparator;
includeSeparator = AppendFlatProperty(builder, nameof(cookie.Secure), cookie.Secure.ToString(), valueSeparator, includeSeparator ? propertySeparator : "") || includeSeparator;
includeSeparator = AppendFlatProperty(builder, nameof(cookie.HttpOnly), cookie.HttpOnly.ToString(), valueSeparator, includeSeparator ? propertySeparator : "") || includeSeparator;
includeSeparator = AppendFlatProperty(builder, nameof(cookie.SameSite), cookie.SameSite.ToString(), valueSeparator, includeSeparator ? propertySeparator : "") || includeSeparator;
}
}
#endif
Expand Down

0 comments on commit 61f8567

Please sign in to comment.