From c1c8a981f42bb0c411f23083cbedc2de2885f3d6 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Thu, 2 Dec 2021 06:55:43 +0100 Subject: [PATCH] Changed format Culture to CultureInfo.InvariantCulture to match default NLog Culture (#735) --- .../AspNetApplicationValueLayoutRenderer.cs | 11 +++++++++-- .../AspNetItemValueLayoutRenderer.cs | 10 +--------- .../AspNetRequestDurationLayoutRenderer.cs | 13 +++++++------ .../AspNetSessionValueLayoutRenderer.cs | 9 +-------- .../AspNetApplicationValueLayoutRendererTests.cs | 4 +++- .../AspNetItemValueLayoutRendererTests.cs | 10 ++++++---- 6 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/NLog.Web/LayoutRenderers/AspNetApplicationValueLayoutRenderer.cs b/src/NLog.Web/LayoutRenderers/AspNetApplicationValueLayoutRenderer.cs index 41258136..82d6623a 100644 --- a/src/NLog.Web/LayoutRenderers/AspNetApplicationValueLayoutRenderer.cs +++ b/src/NLog.Web/LayoutRenderers/AspNetApplicationValueLayoutRenderer.cs @@ -43,6 +43,12 @@ public class AspNetApplicationValueLayoutRenderer : AspNetLayoutRendererBase [DefaultParameter] public string Variable { get; set; } + /// + /// Gets or sets the culture used for rendering. + /// + /// + public CultureInfo Culture { get; set; } = CultureInfo.InvariantCulture; + /// /// Renders the specified ASP.NET Application variable and appends it to the specified . /// @@ -60,8 +66,9 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { return; } - - builder.Append(Convert.ToString(application[Variable], CultureInfo.CurrentUICulture)); + + var culture = GetFormatProvider(logEvent, Culture); + builder.Append(Convert.ToString(application[Variable], culture)); } } } \ No newline at end of file diff --git a/src/Shared/LayoutRenderers/AspNetItemValueLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetItemValueLayoutRenderer.cs index 4fd7023d..7ebd3c24 100644 --- a/src/Shared/LayoutRenderers/AspNetItemValueLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetItemValueLayoutRenderer.cs @@ -43,14 +43,6 @@ namespace NLog.Web.LayoutRenderers [LayoutRenderer("aspnet-item")] public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase { - /// - /// Initializes a new instance of the class. - /// - public AspNetItemValueLayoutRenderer() - { - Culture = CultureInfo.CurrentUICulture; - } - /// /// Gets or sets the item variable name. /// @@ -68,7 +60,7 @@ public AspNetItemValueLayoutRenderer() /// Gets or sets the culture used for rendering. /// /// - public CultureInfo Culture { get; set; } + public CultureInfo Culture { get; set; } = CultureInfo.InvariantCulture; /// /// Renders the specified ASP.NET Item value and appends it to the specified . diff --git a/src/Shared/LayoutRenderers/AspNetRequestDurationLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetRequestDurationLayoutRenderer.cs index 6f215b4d..c17552eb 100644 --- a/src/Shared/LayoutRenderers/AspNetRequestDurationLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetRequestDurationLayoutRenderer.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.Linq; using System.Text; using NLog.Config; @@ -31,14 +32,14 @@ public class AspNetRequestDurationLayoutRenderer : AspNetLayoutRendererBase /// Gets or sets the culture used for rendering. /// /// - public System.Globalization.CultureInfo Culture { get; set; } = System.Globalization.CultureInfo.InvariantCulture; + public CultureInfo Culture { get; set; } = CultureInfo.InvariantCulture; /// protected override void InitializeLayoutRenderer() { - if (DurationMsFormat == null && string.IsNullOrEmpty(Format) && ReferenceEquals(Culture, System.Globalization.CultureInfo.InvariantCulture)) + if (DurationMsFormat == null && string.IsNullOrEmpty(Format) && ReferenceEquals(Culture, CultureInfo.InvariantCulture)) { - System.Threading.Interlocked.CompareExchange(ref DurationMsFormat, Enumerable.Range(0, 1000).Select(i => i.ToString(System.Globalization.CultureInfo.InvariantCulture)).ToArray(), null); + System.Threading.Interlocked.CompareExchange(ref DurationMsFormat, Enumerable.Range(0, 1000).Select(i => i.ToString(CultureInfo.InvariantCulture)).ToArray(), null); } #if !ASP_NET_CORE @@ -47,9 +48,9 @@ protected override void InitializeLayoutRenderer() _formatString = "{0:" + Format + "}"; } #elif ASP_NET_CORE2 - if (string.IsNullOrEmpty(Format) && ReferenceEquals(Culture, System.Globalization.CultureInfo.InvariantCulture)) + if (string.IsNullOrEmpty(Format) && ReferenceEquals(Culture, CultureInfo.InvariantCulture)) _scopeTiming = new NLog.Layouts.SimpleLayout("${scopetiming}"); - else if (ReferenceEquals(Culture, System.Globalization.CultureInfo.InvariantCulture)) + else if (ReferenceEquals(Culture, CultureInfo.InvariantCulture)) _scopeTiming = new NLog.Layouts.SimpleLayout($"${{scopetiming:Format={Format}}}"); else _scopeTiming = new NLog.Layouts.SimpleLayout($"${{scopetiming:Format={Format}:Culture={Culture}}}"); @@ -92,7 +93,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) private void RenderDurationMs(StringBuilder builder, double durationMs) { - if (ReferenceEquals(Culture, System.Globalization.CultureInfo.InvariantCulture)) + if (ReferenceEquals(Culture, CultureInfo.InvariantCulture)) { var truncateMs = (long)durationMs; if (DurationMsFormat != null && truncateMs >= 0 && truncateMs <= DurationMsFormat.Length) diff --git a/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs index c7cd0549..692ecb32 100644 --- a/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs @@ -44,13 +44,6 @@ public class AspNetSessionValueLayoutRenderer : AspNetLayoutRendererBase #if ASP_NET_CORE private const string NLogRetrievingSessionValue = "NLogRetrievingSessionValue"; #endif - /// - /// Initializes a new instance of the class. - /// - public AspNetSessionValueLayoutRenderer() - { - Culture = CultureInfo.CurrentUICulture; - } /// /// Gets or sets the session variable name. @@ -69,7 +62,7 @@ public AspNetSessionValueLayoutRenderer() /// Gets or sets the culture used for rendering. /// /// - public CultureInfo Culture { get; set; } + public CultureInfo Culture { get; set; } = CultureInfo.InvariantCulture; #if ASP_NET_CORE /// diff --git a/tests/NLog.Web.Tests/LayoutRenderers/AspNetApplicationValueLayoutRendererTests.cs b/tests/NLog.Web.Tests/LayoutRenderers/AspNetApplicationValueLayoutRendererTests.cs index 38d3ef7a..f4bca2e1 100644 --- a/tests/NLog.Web.Tests/LayoutRenderers/AspNetApplicationValueLayoutRendererTests.cs +++ b/tests/NLog.Web.Tests/LayoutRenderers/AspNetApplicationValueLayoutRendererTests.cs @@ -63,13 +63,15 @@ public void VariableFoundRendersValue(object expectedValue) var httpContext = Substitute.For(); httpContext.Application["key"].Returns(expectedValue); + var culture = CultureInfo.CurrentUICulture; var renderer = new AspNetApplicationValueLayoutRenderer(); renderer.Variable = "key"; renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + renderer.Culture = culture; string result = renderer.Render(new LogEventInfo()); - Assert.Equal(Convert.ToString(expectedValue, CultureInfo.CurrentUICulture), result); + Assert.Equal(Convert.ToString(expectedValue, culture), result); } public static IEnumerable VariableFoundData diff --git a/tests/Shared/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs b/tests/Shared/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs index 0c0a4d64..5124e671 100644 --- a/tests/Shared/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs +++ b/tests/Shared/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs @@ -84,14 +84,15 @@ public void VariableFoundRendersValue(object expectedValue) httpContext.Items.Contains("key").Returns(true); httpContext.Items["key"].Returns(expectedValue); #endif - + var culture = CultureInfo.CurrentUICulture; renderer.Variable = "key"; + renderer.Culture = culture; // Act string result = renderer.Render(new LogEventInfo()); // Assert - Assert.Equal(Convert.ToString(expectedValue, CultureInfo.CurrentUICulture), result); + Assert.Equal(Convert.ToString(expectedValue, culture), result); } [Theory, MemberData(nameof(NestedPropertyData))] @@ -106,15 +107,16 @@ public void NestedPropertyRendersValue(string itemKey, string variable, object d httpContext.Items.Contains(itemKey).Returns(true); httpContext.Items[itemKey].Returns(data); #endif - + var culture = CultureInfo.CurrentUICulture; renderer.Variable = variable; renderer.EvaluateAsNestedProperties = true; + renderer.Culture = culture; // Act string result = renderer.Render(new LogEventInfo()); // Assert - Assert.Equal(Convert.ToString(expectedValue, CultureInfo.CurrentUICulture), result); + Assert.Equal(Convert.ToString(expectedValue, culture), result); } public static IEnumerable VariableFoundData