Skip to content

Commit

Permalink
Introducing Items-property as DefaultParameter for consistency (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Dec 18, 2022
1 parent 6a2c203 commit db0af2b
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#if ASP_NET_CORE3
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using NLog.LayoutRenderers;
using NLog.Web.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
{
Expand All @@ -30,7 +31,14 @@ public class AspNetRequestTrailersLayoutRenderer : AspNetLayoutMultiValueRendere
/// Trailer names to be rendered.
/// If <c>null</c> or empty array, all trailers will be rendered.
/// </summary>
public List<string> TrailerNames { get; set; }
[DefaultParameter]
public List<string> Items { get; set; }

/// <summary>
/// Trailer names to be rendered.
/// If <c>null</c> or empty array, all trailers will be rendered.
/// </summary>
public List<string> TrailerNames { get => Items; set => Items = value; }

/// <summary>
/// Gets or sets the keys to exclude from the output. If omitted, none are excluded.
Expand Down Expand Up @@ -61,15 +69,15 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
var trailers = httpRequestTrailers.Trailers;
if (trailers?.Count > 0)
{
bool checkForExclude = (TrailerNames == null || TrailerNames.Count == 0) && Exclude?.Count > 0;
bool checkForExclude = (Items == null || Items.Count == 0) && Exclude?.Count > 0;
var headerValues = GetTrailerValues(trailers, checkForExclude);
SerializePairs(headerValues, builder, logEvent);
}
}

private IEnumerable<KeyValuePair<string, string>> GetTrailerValues(IHeaderDictionary trailers, bool checkForExclude)
{
var trailerNames = TrailerNames?.Count > 0 ? TrailerNames : trailers.Keys;
var trailerNames = Items?.Count > 0 ? Items : trailers.Keys;
foreach (var trailerName in trailerNames)
{
if (checkForExclude && Exclude.Contains(trailerName))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#if ASP_NET_CORE3
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;
using System;
using System.Collections.Generic;
using System.Text;

namespace NLog.Web.LayoutRenderers
{
Expand All @@ -29,7 +30,14 @@ public class AspNetResponseTrailersLayoutRenderer : AspNetLayoutMultiValueRender
/// Trailer names to be rendered.
/// If <c>null</c> or empty array, all trailers will be rendered.
/// </summary>
public List<string> TrailerNames { get; set; }
[DefaultParameter]
public List<string> Items { get; set; }

/// <summary>
/// Trailer names to be rendered.
/// If <c>null</c> or empty array, all trailers will be rendered.
/// </summary>
public List<string> TrailerNames { get => Items; set => Items = value; }

/// <summary>
/// Gets or sets the keys to exclude from the output. If omitted, none are excluded.
Expand All @@ -54,15 +62,15 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
var trailers = httpResponseTrailers.Trailers;
if (trailers?.Count > 0)
{
bool checkForExclude = (TrailerNames == null || TrailerNames.Count == 0) && Exclude?.Count > 0;
bool checkForExclude = (Items == null || Items.Count == 0) && Exclude?.Count > 0;
var headerValues = GetTrailerValues(trailers, checkForExclude);
SerializePairs(headerValues, builder, logEvent);
}
}

private IEnumerable<KeyValuePair<string, string>> GetTrailerValues(IHeaderDictionary trailers, bool checkForExclude)
{
var trailerNames = TrailerNames?.Count > 0 ? TrailerNames : trailers.Keys;
var trailerNames = Items?.Count > 0 ? Items : trailers.Keys;
foreach (var trailerName in trailerNames)
{
if (checkForExclude && Exclude.Contains(trailerName))
Expand Down
20 changes: 14 additions & 6 deletions src/Shared/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog.LayoutRenderers;
using NLog.Web.Internal;
#if !ASP_NET_CORE
using NLog.Web.Enums;
using System.Web;
using NLog.Web.Enums;
#else
using Microsoft.AspNetCore.Http;
#endif
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
{
Expand All @@ -33,7 +34,14 @@ public class AspNetRequestCookieLayoutRenderer : AspNetLayoutMultiValueRendererB
/// Cookie names to be rendered.
/// If <c>null</c> or empty array, all cookies will be rendered.
/// </summary>
public List<string> CookieNames { get; set; }
[DefaultParameter]
public List<string> Items { get; set; }

/// <summary>
/// Cookie names to be rendered.
/// If <c>null</c> or empty array, all cookies will be rendered.
/// </summary>
public List<string> CookieNames { get => Items; set => Items = value; }

/// <summary>
/// Gets or sets the keys to exclude from the output. If omitted, none are excluded.
Expand Down Expand Up @@ -79,9 +87,9 @@ private IEnumerable<KeyValuePair<string, string>> GetCookieValues(HttpCookieColl
#else
private IEnumerable<KeyValuePair<string, string>> GetCookieValues(IRequestCookieCollection cookies)
{
if (CookieNames?.Count > 0)
if (Items?.Count > 0)
{
foreach (var cookieName in CookieNames)
foreach (var cookieName in Items)
{
if (cookies.TryGetValue(cookieName, out var cookieValue))
{
Expand Down
20 changes: 14 additions & 6 deletions src/Shared/LayoutRenderers/AspNetRequestHeadersLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog.LayoutRenderers;
using NLog.Web.Internal;
#if !ASP_NET_CORE
using System.Collections.Specialized;
#else
using Microsoft.AspNetCore.Http;
#endif
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
{
Expand All @@ -32,7 +33,14 @@ public class AspNetRequestHeadersLayoutRenderer : AspNetLayoutMultiValueRenderer
/// Header names to be rendered.
/// If <c>null</c> or empty array, all headers will be rendered.
/// </summary>
public List<string> HeaderNames { get; set; }
[DefaultParameter]
public List<string> Items { get; set; }

/// <summary>
/// Header names to be rendered.
/// If <c>null</c> or empty array, all headers will be rendered.
/// </summary>
public List<string> HeaderNames { get => Items; set => Items = value; }

/// <summary>
/// Gets or sets the keys to exclude from the output. If omitted, none are excluded.
Expand Down Expand Up @@ -64,7 +72,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
var headers = httpRequest.Headers;
if (headers?.Count > 0)
{
bool checkForExclude = (HeaderNames == null || HeaderNames.Count == 0) && Exclude?.Count > 0;
bool checkForExclude = (Items == null || Items.Count == 0) && Exclude?.Count > 0;
var headerValues = GetHeaderValues(headers, checkForExclude);
SerializePairs(headerValues, builder, logEvent);
}
Expand All @@ -73,7 +81,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
#if !ASP_NET_CORE
private IEnumerable<KeyValuePair<string, string>> GetHeaderValues(NameValueCollection headers, bool checkForExclude)
{
var headerNames = HeaderNames?.Count > 0 ? HeaderNames : headers.Keys.Cast<string>();
var headerNames = Items?.Count > 0 ? Items : headers.Keys.Cast<string>();
foreach (var headerName in headerNames)
{
if (checkForExclude && Exclude.Contains(headerName))
Expand All @@ -91,7 +99,7 @@ private IEnumerable<KeyValuePair<string, string>> GetHeaderValues(NameValueColle
#else
private IEnumerable<KeyValuePair<string, string>> GetHeaderValues(IHeaderDictionary headers, bool checkForExclude)
{
var headerNames = HeaderNames?.Count > 0 ? HeaderNames : headers.Keys;
var headerNames = Items?.Count > 0 ? Items : headers.Keys;
foreach (var headerName in headerNames)
{
if (checkForExclude && Exclude.Contains(headerName))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
using NLog.LayoutRenderers;
using NLog.Web.Internal;
#if !ASP_NET_CORE
using System.Collections.Specialized;
#else
using Microsoft.AspNetCore.Http;
#endif
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
{
Expand All @@ -29,7 +30,14 @@ public class AspNetQueryStringLayoutRenderer : AspNetLayoutMultiValueRendererBas
/// List Query Strings' Key to be rendered from Request.
/// If empty, then render all querystrings
/// </summary>
public List<string> QueryStringKeys { get; set; }
[DefaultParameter]
public List<string> Items { get; set; }

/// <summary>
/// List Query Strings' Key to be rendered from Request.
/// If empty, then render all querystrings
/// </summary>
public List<string> QueryStringKeys { get => Items; set => Items = value; }

/// <inheritdoc/>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
Expand All @@ -48,7 +56,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
if (queryStrings == null || queryStrings.Count == 0)
return;

var queryStringKeys = QueryStringKeys;
var queryStringKeys = Items;
var printAllQueryString = queryStringKeys == null || queryStringKeys.Count == 0;
if (printAllQueryString)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Routing;
using HttpContextBase = Microsoft.AspNetCore.Http.HttpContext;
#endif
using NLog.Config;
using NLog.LayoutRenderers;

namespace NLog.Web.LayoutRenderers
Expand All @@ -29,13 +30,14 @@ public class AspNetRequestRouteParametersRenderer : AspNetLayoutMultiValueRender
/// List Route Parameter' Key to be rendered from Request.
/// If empty, then render all parameters
/// </summary>
public List<string> RouteParameterKeys { get => Items; set => Items = value; }
[DefaultParameter]
public List<string> Items { get; set; }

/// <summary>
/// List Route Parameter' Key to be rendered from Request.
/// If empty, then render all parameters
/// </summary>
public List<string> Items { get; set; }
public List<string> RouteParameterKeys { get => Items; set => Items = value; }

/// <inheritdoc/>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
Expand Down
31 changes: 20 additions & 11 deletions src/Shared/LayoutRenderers/AspNetResponseCookieLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog.LayoutRenderers;
using NLog.Web.Enums;
using NLog.Web.Internal;
using NLog.Layouts;
#if !ASP_NET_CORE
using System.Web;
#else
Expand All @@ -14,6 +10,12 @@
using Microsoft.AspNetCore.Http;
#endif

using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Layouts;
using NLog.Web.Enums;
using NLog.Web.Internal;

namespace NLog.Web.LayoutRenderers
{
/// <summary>
Expand Down Expand Up @@ -43,7 +45,14 @@ public class AspNetResponseCookieLayoutRenderer : AspNetLayoutMultiValueRenderer
/// Cookie names to be rendered.
/// If <c>null</c> or empty array, all cookies will be rendered.
/// </summary>
public List<string> CookieNames { get; set; }
[DefaultParameter]
public List<string> Items { get; set; }

/// <summary>
/// Cookie names to be rendered.
/// If <c>null</c> or empty array, all cookies will be rendered.
/// </summary>
public List<string> CookieNames { get => Items; set => Items = value; }

/// <summary>
/// Render all of the cookie properties, such as Daom and Path, not merely Name and Value
Expand Down Expand Up @@ -157,13 +166,13 @@ private HttpCookieCollection GetCookies(HttpResponseBase response)
private IEnumerable<KeyValuePair<string, string>> GetCookieValues(HttpCookieCollection cookies)
{
var expandMultiValue = OutputFormat != AspNetRequestLayoutOutputFormat.Flat;
return HttpCookieCollectionValues.GetCookieValues(cookies, CookieNames, Exclude, expandMultiValue);
return HttpCookieCollectionValues.GetCookieValues(cookies, Items, Exclude, expandMultiValue);
}

private IEnumerable<HttpCookie> GetVerboseCookieValues(HttpCookieCollection cookies)
{
var expandMultiValue = OutputFormat != AspNetRequestLayoutOutputFormat.Flat;
return HttpCookieCollectionValues.GetVerboseCookieValues(cookies, CookieNames, Exclude, expandMultiValue);
return HttpCookieCollectionValues.GetVerboseCookieValues(cookies, Items, Exclude, expandMultiValue);
}

private void SerializeAllProperties(IEnumerable<HttpCookie> verboseCookieValues, StringBuilder builder, LogEventInfo logEvent)
Expand Down Expand Up @@ -274,9 +283,9 @@ private static IList<SetCookieHeaderValue> GetCookies(HttpResponse response)

private IEnumerable<KeyValuePair<string, string>> GetCookieValues(IList<SetCookieHeaderValue> cookies)
{
if (CookieNames?.Count > 0)
if (Items?.Count > 0)
{
return GetCookieNameValues(cookies, CookieNames);
return GetCookieNameValues(cookies, Items);
}
else
{
Expand All @@ -286,9 +295,9 @@ private IEnumerable<KeyValuePair<string, string>> GetCookieValues(IList<SetCooki

private IEnumerable<SetCookieHeaderValue> GetVerboseCookieValues(IList<SetCookieHeaderValue> cookies)
{
if (CookieNames?.Count > 0)
if (Items?.Count > 0)
{
return GetCookieVerboseValues(cookies, CookieNames);
return GetCookieVerboseValues(cookies, Items);
}
else
{
Expand Down
Loading

0 comments on commit db0af2b

Please sign in to comment.