From 8edc465c8f85b4e827cefcad3da54bc5f7f57a0e Mon Sep 17 00:00:00 2001 From: Sebastian Bebrys Date: Thu, 30 Mar 2017 09:34:10 +0200 Subject: [PATCH 1/6] Correct use configured culture Correct use configured culture in HttpContext Items and SessionId --- .../AspNetItemValueLayoutRendererTests.cs | 26 +++++++++++++++-- NLog.Web.AspNetCore.Tests/project.json | 4 +-- .../AspNetItemValueLayoutRenderer.cs | 28 +++++++++++++----- .../AspNetSessionValueLayoutRenderer.cs | 29 ++++++++++++++----- 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs b/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs index 1e08b0cc..0503b92a 100644 --- a/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs +++ b/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs @@ -55,7 +55,29 @@ public void VariableNotFoundRendersEmptyString() Assert.Empty(result); } - [Theory, MemberData("VariableFoundData")] + [Theory, MemberData("VariableFoundData")] + public void CulturedVariableFoundRendersValue(object expectedValue) + { + var httpContext = Substitute.For(); +#if NETSTANDARD_1plus + httpContext.Items = new Dictionary(); + httpContext.Items.Add("key", expectedValue); +#else + httpContext.Items["key"].Returns(expectedValue); +#endif + var cultureInfo = new CultureInfo("nl-NL"); + var renderer = new AspNetItemValueLayoutRenderer(); + renderer.Variable = "key"; + renderer.Culture = cultureInfo; + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + + string result = renderer.Render(new LogEventInfo()); + + Assert.Equal(Convert.ToString(expectedValue, cultureInfo), result); + } + + + [Theory, MemberData("VariableFoundData")] public void VariableFoundRendersValue(object expectedValue) { var httpContext = Substitute.For(); @@ -75,7 +97,7 @@ public void VariableFoundRendersValue(object expectedValue) Assert.Equal(Convert.ToString(expectedValue, CultureInfo.CurrentUICulture), result); } - [Theory, MemberData("NestedPropertyData")] + [Theory, MemberData("NestedPropertyData")] public void NestedPropertyRendersValue(string itemKey, string variable, object data, object expectedValue) { var httpContext = Substitute.For(); diff --git a/NLog.Web.AspNetCore.Tests/project.json b/NLog.Web.AspNetCore.Tests/project.json index a4d0b7ce..6cd0a8fb 100644 --- a/NLog.Web.AspNetCore.Tests/project.json +++ b/NLog.Web.AspNetCore.Tests/project.json @@ -5,8 +5,8 @@ "Microsoft.AspNetCore.Hosting.Abstractions": "1.0.0", "Microsoft.AspNetCore.Http.Extensions": "1.0.0", "Microsoft.AspNetCore.Routing.Abstractions": "1.0.0", - "NLog": "5.0.0-beta04", - "NLog.Web.AspNetCore": "4.3.0", + "NLog": "5.0.0-beta05", + "NLog.Web.AspNetCore": "4.3.2", "NSubstitute": "2.0.0-alpha003", "xunit": "2.2.0-beta2-build3300", "dotnet-test-xunit": "2.2.0-preview2-build1029" diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetItemValueLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetItemValueLayoutRenderer.cs index 3eea0e14..05517fbf 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetItemValueLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetItemValueLayoutRenderer.cs @@ -41,6 +41,13 @@ namespace NLog.Web.LayoutRenderers [LayoutRenderer("aspnet-item")] public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase { + /// + /// Initializes a new instance of the class. + /// + public AspNetItemValueLayoutRenderer() + { + this.Culture = CultureInfo.CurrentUICulture; + } /// /// Gets or sets the item variable name. @@ -55,12 +62,18 @@ public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase /// public bool EvaluateAsNestedProperties { get; set; } - /// - /// Renders the specified ASP.NET Item value and appends it to the specified . - /// - /// The to append the rendered data to. - /// Logging event. - protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) + /// + /// Gets or sets the culture used for rendering. + /// + /// + public CultureInfo Culture { get; set; } + + /// + /// Renders the specified ASP.NET Item value and appends it to the specified . + /// + /// The to append the rendered data to. + /// Logging event. + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { if (Variable == null) { @@ -76,8 +89,9 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) Func getVal = k => context.Items[k]; var value = PropertyReader.GetValue(Variable, getVal, EvaluateAsNestedProperties); + var formatProvider = GetFormatProvider(logEvent, Culture); - builder.Append(Convert.ToString(value, CultureInfo.CurrentUICulture)); + builder.Append(Convert.ToString(value, formatProvider)); } } } \ No newline at end of file diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs index 958eb514..27797a54 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs @@ -44,6 +44,13 @@ namespace NLog.Web.LayoutRenderers [LayoutRenderer("aspnet-session")] public class AspNetSessionValueLayoutRenderer : AspNetLayoutRendererBase { + /// + /// Initializes a new instance of the class. + /// + public AspNetSessionValueLayoutRenderer() + { + this.Culture = CultureInfo.CurrentUICulture; + } /// /// Gets or sets the session variable name. @@ -58,12 +65,18 @@ public class AspNetSessionValueLayoutRenderer : AspNetLayoutRendererBase /// public bool EvaluateAsNestedProperties { get; set; } - /// - /// Renders the specified ASP.NET Session value and appends it to the specified . - /// - /// The to append the rendered data to. - /// Logging event. - protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) + /// + /// Gets or sets the culture used for rendering. + /// + /// + public CultureInfo Culture { get; set; } + + /// + /// Renders the specified ASP.NET Session value and appends it to the specified . + /// + /// The to append the rendered data to. + /// Logging event. + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { if (this.Variable == null) { @@ -115,8 +128,8 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) #endif - - builder.Append(Convert.ToString(value, CultureInfo.CurrentUICulture)); + var formatProvider = GetFormatProvider(logEvent, Culture); + builder.Append(Convert.ToString(value, formatProvider)); } private const string NLogRetrievingSessionValue = "NLogRetrievingSessionValue"; From a3635dddbfd6775954af7a8df2cf73393fbb52de Mon Sep 17 00:00:00 2001 From: Sebastian Bebrys Date: Thu, 30 Mar 2017 09:37:09 +0200 Subject: [PATCH 2/6] Correct tabs --- .../AspNetItemValueLayoutRendererTests.cs | 34 ++++++++-------- .../AspNetItemValueLayoutRenderer.cs | 40 +++++++++---------- .../AspNetSessionValueLayoutRenderer.cs | 40 +++++++++---------- 3 files changed, 57 insertions(+), 57 deletions(-) diff --git a/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs b/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs index 0503b92a..f53b47c3 100644 --- a/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs +++ b/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs @@ -55,29 +55,29 @@ public void VariableNotFoundRendersEmptyString() Assert.Empty(result); } - [Theory, MemberData("VariableFoundData")] - public void CulturedVariableFoundRendersValue(object expectedValue) - { - var httpContext = Substitute.For(); + [Theory, MemberData("VariableFoundData")] + public void CulturedVariableFoundRendersValue(object expectedValue) + { + var httpContext = Substitute.For(); #if NETSTANDARD_1plus httpContext.Items = new Dictionary(); httpContext.Items.Add("key", expectedValue); #else - httpContext.Items["key"].Returns(expectedValue); + httpContext.Items["key"].Returns(expectedValue); #endif - var cultureInfo = new CultureInfo("nl-NL"); - var renderer = new AspNetItemValueLayoutRenderer(); - renderer.Variable = "key"; - renderer.Culture = cultureInfo; - renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); + var cultureInfo = new CultureInfo("nl-NL"); + var renderer = new AspNetItemValueLayoutRenderer(); + renderer.Variable = "key"; + renderer.Culture = cultureInfo; + renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); - string result = renderer.Render(new LogEventInfo()); + string result = renderer.Render(new LogEventInfo()); - Assert.Equal(Convert.ToString(expectedValue, cultureInfo), result); - } + Assert.Equal(Convert.ToString(expectedValue, cultureInfo), result); + } - [Theory, MemberData("VariableFoundData")] + [Theory, MemberData("VariableFoundData")] public void VariableFoundRendersValue(object expectedValue) { var httpContext = Substitute.For(); @@ -85,9 +85,9 @@ public void VariableFoundRendersValue(object expectedValue) httpContext.Items = new Dictionary(); httpContext.Items.Add("key", expectedValue); #else - httpContext.Items["key"].Returns(expectedValue); + httpContext.Items["key"].Returns(expectedValue); #endif - + var renderer = new AspNetItemValueLayoutRenderer(); renderer.Variable = "key"; renderer.HttpContextAccessor = new FakeHttpContextAccessor(httpContext); @@ -97,7 +97,7 @@ public void VariableFoundRendersValue(object expectedValue) Assert.Equal(Convert.ToString(expectedValue, CultureInfo.CurrentUICulture), result); } - [Theory, MemberData("NestedPropertyData")] + [Theory, MemberData("NestedPropertyData")] public void NestedPropertyRendersValue(string itemKey, string variable, object data, object expectedValue) { var httpContext = Substitute.For(); diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetItemValueLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetItemValueLayoutRenderer.cs index 05517fbf..7ab9af35 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetItemValueLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetItemValueLayoutRenderer.cs @@ -41,13 +41,13 @@ namespace NLog.Web.LayoutRenderers [LayoutRenderer("aspnet-item")] public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase { - /// - /// Initializes a new instance of the class. - /// - public AspNetItemValueLayoutRenderer() - { - this.Culture = CultureInfo.CurrentUICulture; - } + /// + /// Initializes a new instance of the class. + /// + public AspNetItemValueLayoutRenderer() + { + this.Culture = CultureInfo.CurrentUICulture; + } /// /// Gets or sets the item variable name. @@ -62,18 +62,18 @@ public AspNetItemValueLayoutRenderer() /// public bool EvaluateAsNestedProperties { get; set; } - /// - /// Gets or sets the culture used for rendering. - /// - /// - public CultureInfo Culture { get; set; } + /// + /// Gets or sets the culture used for rendering. + /// + /// + public CultureInfo Culture { get; set; } - /// - /// Renders the specified ASP.NET Item value and appends it to the specified . - /// - /// The to append the rendered data to. - /// Logging event. - protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) + /// + /// Renders the specified ASP.NET Item value and appends it to the specified . + /// + /// The to append the rendered data to. + /// Logging event. + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { if (Variable == null) { @@ -89,9 +89,9 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) Func getVal = k => context.Items[k]; var value = PropertyReader.GetValue(Variable, getVal, EvaluateAsNestedProperties); - var formatProvider = GetFormatProvider(logEvent, Culture); + var formatProvider = GetFormatProvider(logEvent, Culture); - builder.Append(Convert.ToString(value, formatProvider)); + builder.Append(Convert.ToString(value, formatProvider)); } } } \ No newline at end of file diff --git a/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs b/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs index 27797a54..ad439671 100644 --- a/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs +++ b/NLog.Web.AspNetCore/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs @@ -44,13 +44,13 @@ namespace NLog.Web.LayoutRenderers [LayoutRenderer("aspnet-session")] public class AspNetSessionValueLayoutRenderer : AspNetLayoutRendererBase { - /// - /// Initializes a new instance of the class. - /// - public AspNetSessionValueLayoutRenderer() - { - this.Culture = CultureInfo.CurrentUICulture; - } + /// + /// Initializes a new instance of the class. + /// + public AspNetSessionValueLayoutRenderer() + { + this.Culture = CultureInfo.CurrentUICulture; + } /// /// Gets or sets the session variable name. @@ -65,18 +65,18 @@ public AspNetSessionValueLayoutRenderer() /// public bool EvaluateAsNestedProperties { get; set; } - /// - /// Gets or sets the culture used for rendering. - /// - /// - public CultureInfo Culture { get; set; } + /// + /// Gets or sets the culture used for rendering. + /// + /// + public CultureInfo Culture { get; set; } - /// - /// Renders the specified ASP.NET Session value and appends it to the specified . - /// - /// The to append the rendered data to. - /// Logging event. - protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) + /// + /// Renders the specified ASP.NET Session value and appends it to the specified . + /// + /// The to append the rendered data to. + /// Logging event. + protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { if (this.Variable == null) { @@ -128,8 +128,8 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) #endif - var formatProvider = GetFormatProvider(logEvent, Culture); - builder.Append(Convert.ToString(value, formatProvider)); + var formatProvider = GetFormatProvider(logEvent, Culture); + builder.Append(Convert.ToString(value, formatProvider)); } private const string NLogRetrievingSessionValue = "NLogRetrievingSessionValue"; From b3f802f6a4551d6c0e349af8c12556d8542cb48b Mon Sep 17 00:00:00 2001 From: Sebastian Bebrys Date: Thu, 30 Mar 2017 09:53:14 +0200 Subject: [PATCH 3/6] Remove DNX in NetCoreTest --- NLog.Web.AspNetCore/NLog.Web.AspNetCore.xproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NLog.Web.AspNetCore/NLog.Web.AspNetCore.xproj b/NLog.Web.AspNetCore/NLog.Web.AspNetCore.xproj index c0cd10f5..e53469f6 100644 --- a/NLog.Web.AspNetCore/NLog.Web.AspNetCore.xproj +++ b/NLog.Web.AspNetCore/NLog.Web.AspNetCore.xproj @@ -4,7 +4,7 @@ 14.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - + 74d5915b-bea9-404c-b4d0-b663164def37 NLog.Web @@ -18,5 +18,5 @@ True - + \ No newline at end of file From ed1aa49eacdf95df7bde7c98e9a0a13edb898ce3 Mon Sep 17 00:00:00 2001 From: sebe Date: Thu, 30 Mar 2017 23:25:45 +0200 Subject: [PATCH 4/6] Adding global.json to NetCoreTest --- NLog.Web.AspNetCore.Tests/global.json | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 NLog.Web.AspNetCore.Tests/global.json diff --git a/NLog.Web.AspNetCore.Tests/global.json b/NLog.Web.AspNetCore.Tests/global.json new file mode 100644 index 00000000..cc7fab88 --- /dev/null +++ b/NLog.Web.AspNetCore.Tests/global.json @@ -0,0 +1,6 @@ +{ + "sdk": { + "version": "1.0.0-preview2-1-003177" + + } +} From f5eea1e1dad412669a7439a6a359ddbe592e461f Mon Sep 17 00:00:00 2001 From: sebe Date: Fri, 31 Mar 2017 00:12:02 +0200 Subject: [PATCH 5/6] Cleaning global.json-s --- NLog.Web.AspNetCore/global.json | 6 ------ NLog.Web.AspNetCore.Tests/global.json => global.json | 0 2 files changed, 6 deletions(-) delete mode 100644 NLog.Web.AspNetCore/global.json rename NLog.Web.AspNetCore.Tests/global.json => global.json (100%) diff --git a/NLog.Web.AspNetCore/global.json b/NLog.Web.AspNetCore/global.json deleted file mode 100644 index cc7fab88..00000000 --- a/NLog.Web.AspNetCore/global.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "sdk": { - "version": "1.0.0-preview2-1-003177" - - } -} diff --git a/NLog.Web.AspNetCore.Tests/global.json b/global.json similarity index 100% rename from NLog.Web.AspNetCore.Tests/global.json rename to global.json From c9292d76ea31ff8a7073535f45a2d458b4445736 Mon Sep 17 00:00:00 2001 From: sebe Date: Fri, 31 Mar 2017 00:29:31 +0200 Subject: [PATCH 6/6] Correct cultured dateTime render in test --- .../LayoutRenderers/AspNetSessionValueLayoutRendererTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetSessionValueLayoutRendererTests.cs b/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetSessionValueLayoutRendererTests.cs index 4f255206..07a6be2a 100644 --- a/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetSessionValueLayoutRendererTests.cs +++ b/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetSessionValueLayoutRendererTests.cs @@ -192,7 +192,7 @@ public void SessionWithCulture() var o = new { b = new DateTime(2015, 11, 24, 2, 30, 23) }; //set in "a" - ExecTest("a", o, "11/24/2015 2:30:23 AM", layout); + ExecTest("a", o, "24/11/2015 02:30:23", layout); } ///