diff --git a/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestTrailersLayoutRenderer.cs b/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestTrailersLayoutRenderer.cs index 7a5de3e1..624f4c86 100644 --- a/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestTrailersLayoutRenderer.cs +++ b/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestTrailersLayoutRenderer.cs @@ -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 { @@ -30,7 +31,14 @@ public class AspNetRequestTrailersLayoutRenderer : AspNetLayoutMultiValueRendere /// Trailer names to be rendered. /// If null or empty array, all trailers will be rendered. /// - public List TrailerNames { get; set; } + [DefaultParameter] + public List Items { get; set; } + + /// + /// Trailer names to be rendered. + /// If null or empty array, all trailers will be rendered. + /// + public List TrailerNames { get => Items; set => Items = value; } /// /// Gets or sets the keys to exclude from the output. If omitted, none are excluded. @@ -61,7 +69,7 @@ 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); } @@ -69,7 +77,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) private IEnumerable> 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)) diff --git a/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetResponseTrailersLayoutRenderer.cs b/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetResponseTrailersLayoutRenderer.cs index fa8780bb..01a8b9f4 100644 --- a/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetResponseTrailersLayoutRenderer.cs +++ b/src/NLog.Web.AspNetCore/LayoutRenderers/AspNetResponseTrailersLayoutRenderer.cs @@ -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 { @@ -29,7 +30,14 @@ public class AspNetResponseTrailersLayoutRenderer : AspNetLayoutMultiValueRender /// Trailer names to be rendered. /// If null or empty array, all trailers will be rendered. /// - public List TrailerNames { get; set; } + [DefaultParameter] + public List Items { get; set; } + + /// + /// Trailer names to be rendered. + /// If null or empty array, all trailers will be rendered. + /// + public List TrailerNames { get => Items; set => Items = value; } /// /// Gets or sets the keys to exclude from the output. If omitted, none are excluded. @@ -54,7 +62,7 @@ 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); } @@ -62,7 +70,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) private IEnumerable> 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)) diff --git a/src/Shared/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs index 1f7fef6f..bd836056 100644 --- a/src/Shared/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetRequestCookieLayoutRenderer.cs @@ -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 { @@ -33,7 +34,14 @@ public class AspNetRequestCookieLayoutRenderer : AspNetLayoutMultiValueRendererB /// Cookie names to be rendered. /// If null or empty array, all cookies will be rendered. /// - public List CookieNames { get; set; } + [DefaultParameter] + public List Items { get; set; } + + /// + /// Cookie names to be rendered. + /// If null or empty array, all cookies will be rendered. + /// + public List CookieNames { get => Items; set => Items = value; } /// /// Gets or sets the keys to exclude from the output. If omitted, none are excluded. @@ -79,9 +87,9 @@ private IEnumerable> GetCookieValues(HttpCookieColl #else private IEnumerable> 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)) { diff --git a/src/Shared/LayoutRenderers/AspNetRequestHeadersLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetRequestHeadersLayoutRenderer.cs index b7d4e38c..5d9c30d6 100644 --- a/src/Shared/LayoutRenderers/AspNetRequestHeadersLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetRequestHeadersLayoutRenderer.cs @@ -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 { @@ -32,7 +33,14 @@ public class AspNetRequestHeadersLayoutRenderer : AspNetLayoutMultiValueRenderer /// Header names to be rendered. /// If null or empty array, all headers will be rendered. /// - public List HeaderNames { get; set; } + [DefaultParameter] + public List Items { get; set; } + + /// + /// Header names to be rendered. + /// If null or empty array, all headers will be rendered. + /// + public List HeaderNames { get => Items; set => Items = value; } /// /// Gets or sets the keys to exclude from the output. If omitted, none are excluded. @@ -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); } @@ -73,7 +81,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) #if !ASP_NET_CORE private IEnumerable> GetHeaderValues(NameValueCollection headers, bool checkForExclude) { - var headerNames = HeaderNames?.Count > 0 ? HeaderNames : headers.Keys.Cast(); + var headerNames = Items?.Count > 0 ? Items : headers.Keys.Cast(); foreach (var headerName in headerNames) { if (checkForExclude && Exclude.Contains(headerName)) @@ -91,7 +99,7 @@ private IEnumerable> GetHeaderValues(NameValueColle #else private IEnumerable> 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)) diff --git a/src/Shared/LayoutRenderers/AspNetRequestQueryStringLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetRequestQueryStringLayoutRenderer.cs index 8c9bca3b..12bb09e0 100644 --- a/src/Shared/LayoutRenderers/AspNetRequestQueryStringLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetRequestQueryStringLayoutRenderer.cs @@ -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 { @@ -29,7 +30,14 @@ public class AspNetQueryStringLayoutRenderer : AspNetLayoutMultiValueRendererBas /// List Query Strings' Key to be rendered from Request. /// If empty, then render all querystrings /// - public List QueryStringKeys { get; set; } + [DefaultParameter] + public List Items { get; set; } + + /// + /// List Query Strings' Key to be rendered from Request. + /// If empty, then render all querystrings + /// + public List QueryStringKeys { get => Items; set => Items = value; } /// protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) @@ -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) { diff --git a/src/Shared/LayoutRenderers/AspNetRequestRouteParametersRenderer.cs b/src/Shared/LayoutRenderers/AspNetRequestRouteParametersRenderer.cs index bedf09c1..95d7e20e 100644 --- a/src/Shared/LayoutRenderers/AspNetRequestRouteParametersRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetRequestRouteParametersRenderer.cs @@ -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 @@ -29,13 +30,14 @@ public class AspNetRequestRouteParametersRenderer : AspNetLayoutMultiValueRender /// List Route Parameter' Key to be rendered from Request. /// If empty, then render all parameters /// - public List RouteParameterKeys { get => Items; set => Items = value; } + [DefaultParameter] + public List Items { get; set; } /// /// List Route Parameter' Key to be rendered from Request. /// If empty, then render all parameters /// - public List Items { get; set; } + public List RouteParameterKeys { get => Items; set => Items = value; } /// protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) diff --git a/src/Shared/LayoutRenderers/AspNetResponseCookieLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetResponseCookieLayoutRenderer.cs index ef755557..64b9bf08 100644 --- a/src/Shared/LayoutRenderers/AspNetResponseCookieLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetResponseCookieLayoutRenderer.cs @@ -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 @@ -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 { /// @@ -43,7 +45,14 @@ public class AspNetResponseCookieLayoutRenderer : AspNetLayoutMultiValueRenderer /// Cookie names to be rendered. /// If null or empty array, all cookies will be rendered. /// - public List CookieNames { get; set; } + [DefaultParameter] + public List Items { get; set; } + + /// + /// Cookie names to be rendered. + /// If null or empty array, all cookies will be rendered. + /// + public List CookieNames { get => Items; set => Items = value; } /// /// Render all of the cookie properties, such as Daom and Path, not merely Name and Value @@ -157,13 +166,13 @@ private HttpCookieCollection GetCookies(HttpResponseBase response) private IEnumerable> GetCookieValues(HttpCookieCollection cookies) { var expandMultiValue = OutputFormat != AspNetRequestLayoutOutputFormat.Flat; - return HttpCookieCollectionValues.GetCookieValues(cookies, CookieNames, Exclude, expandMultiValue); + return HttpCookieCollectionValues.GetCookieValues(cookies, Items, Exclude, expandMultiValue); } private IEnumerable 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 verboseCookieValues, StringBuilder builder, LogEventInfo logEvent) @@ -274,9 +283,9 @@ private static IList GetCookies(HttpResponse response) private IEnumerable> GetCookieValues(IList cookies) { - if (CookieNames?.Count > 0) + if (Items?.Count > 0) { - return GetCookieNameValues(cookies, CookieNames); + return GetCookieNameValues(cookies, Items); } else { @@ -286,9 +295,9 @@ private IEnumerable> GetCookieValues(IList GetVerboseCookieValues(IList cookies) { - if (CookieNames?.Count > 0) + if (Items?.Count > 0) { - return GetCookieVerboseValues(cookies, CookieNames); + return GetCookieVerboseValues(cookies, Items); } else { diff --git a/src/Shared/LayoutRenderers/AspNetResponseHeadersLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetResponseHeadersLayoutRenderer.cs index a2868f36..089f8c26 100644 --- a/src/Shared/LayoutRenderers/AspNetResponseHeadersLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetResponseHeadersLayoutRenderer.cs @@ -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 { @@ -32,7 +33,14 @@ public class AspNetResponseHeadersLayoutRenderer : AspNetLayoutMultiValueRendere /// Header names to be rendered. /// If null or empty array, all headers will be rendered. /// - public List HeaderNames { get; set; } + [DefaultParameter] + public List Items { get; set; } + + /// + /// Header names to be rendered. + /// If null or empty array, all headers will be rendered. + /// + public List HeaderNames { get => Items; set => Items = value; } /// /// Gets or sets the keys to exclude from the output. If omitted, none are excluded. @@ -64,39 +72,39 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) var headers = httpResponse.Headers; if (headers?.Count > 0) { - bool checkForExclude = (HeaderNames == null || HeaderNames.Count == 0) && Exclude?.Count > 0; - var headersDictionary = GetHeaders(headers); - var headerValues = GetHeaderValues(headersDictionary, checkForExclude); + bool checkForExclude = (Items == null || Items.Count == 0) && Exclude?.Count > 0; + var headerValues = GetHeaderValues(headers, checkForExclude); SerializePairs(headerValues, builder, logEvent); } } -#if!ASP_NET_CORE - private static Dictionary GetHeaders(NameValueCollection headers) - { - return headers.Keys.Cast().ToDictionary(headerKey => headerKey, headerKey => headers[headerKey]); - } -#else - private static Dictionary GetHeaders(IHeaderDictionary headers) - { - return headers.Keys.ToDictionary(headerKey => headerKey, headerKey => headers[headerKey]); - } -#endif - private IEnumerable> GetHeaderValues(Dictionary headers, bool checkForExclude) +#if !ASP_NET_CORE + private IEnumerable> GetHeaderValues(NameValueCollection headers, bool checkForExclude) { - var headerNames = HeaderNames?.Count > 0 ? HeaderNames : headers.Keys.ToList(); + var headerNames = Items?.Count > 0 ? Items : headers.Keys.Cast(); foreach (var headerName in headerNames) { if (checkForExclude && Exclude.Contains(headerName)) - { continue; - } - if (!headers.ContainsKey(headerName)) + var headerValue = headers[headerName]; + if (headerValue == null) { continue; } + yield return new KeyValuePair(headerName, headerValue); + } + } +#else + private IEnumerable> GetHeaderValues(IHeaderDictionary headers, bool checkForExclude) + { + var headerNames = Items?.Count > 0 ? Items : headers.Keys; + foreach (var headerName in headerNames) + { + if (checkForExclude && Exclude.Contains(headerName)) + continue; + if (!headers.TryGetValue(headerName, out var headerValue)) { continue; @@ -105,5 +113,6 @@ private IEnumerable> GetHeaderValues(Dictionary(headerName, headerValue); } } +#endif } } \ No newline at end of file