diff --git a/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs b/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs index 1e08b0cc..f53b47c3 100644 --- a/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs +++ b/NLog.Web.AspNetCore.Tests/LayoutRenderers/AspNetItemValueLayoutRendererTests.cs @@ -55,6 +55,28 @@ public void VariableNotFoundRendersEmptyString() Assert.Empty(result); } + [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) { @@ -63,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); 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); } /// 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..7ab9af35 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,6 +62,12 @@ public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase /// public bool EvaluateAsNestedProperties { 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 . /// @@ -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..ad439671 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,6 +65,12 @@ public class AspNetSessionValueLayoutRenderer : AspNetLayoutRendererBase /// public bool EvaluateAsNestedProperties { 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 . /// @@ -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"; 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 diff --git a/NLog.Web.AspNetCore/global.json b/global.json similarity index 100% rename from NLog.Web.AspNetCore/global.json rename to global.json