Skip to content

Commit

Permalink
Added Culture option to ${aspnet-item} and {aspnet-session} (#123)
Browse files Browse the repository at this point in the history
* Correct use configured culture

Correct use configured culture in HttpContext Items and SessionId

* Correct tabs

* Remove DNX in NetCoreTest

* Adding global.json to NetCoreTest

* Cleaning global.json-s

* Correct cultured dateTime render in test
  • Loading branch information
Sebastian Bebrys authored and 304NotModified committed Mar 31, 2017
1 parent c8176a4 commit 2ab8049
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ public void VariableNotFoundRendersEmptyString()
Assert.Empty(result);
}

[Theory, MemberData("VariableFoundData")]
public void CulturedVariableFoundRendersValue(object expectedValue)
{
var httpContext = Substitute.For<HttpContextBase>();
#if NETSTANDARD_1plus
httpContext.Items = new Dictionary<object, object>();
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)
{
Expand All @@ -63,9 +85,9 @@ public void VariableFoundRendersValue(object expectedValue)
httpContext.Items = new Dictionary<object, object>();
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions NLog.Web.AspNetCore.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ namespace NLog.Web.LayoutRenderers
[LayoutRenderer("aspnet-item")]
public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase
{
/// <summary>
/// Initializes a new instance of the <see cref="AspNetItemValueLayoutRenderer" /> class.
/// </summary>
public AspNetItemValueLayoutRenderer()
{
this.Culture = CultureInfo.CurrentUICulture;
}

/// <summary>
/// Gets or sets the item variable name.
Expand All @@ -55,6 +62,12 @@ public class AspNetItemValueLayoutRenderer : AspNetLayoutRendererBase
/// <docgen category='Rendering Options' order='10' />
public bool EvaluateAsNestedProperties { get; set; }

/// <summary>
/// Gets or sets the culture used for rendering.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public CultureInfo Culture { get; set; }

/// <summary>
/// Renders the specified ASP.NET Item value and appends it to the specified <see cref="StringBuilder" />.
/// </summary>
Expand All @@ -76,8 +89,9 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
Func<string, object> 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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ namespace NLog.Web.LayoutRenderers
[LayoutRenderer("aspnet-session")]
public class AspNetSessionValueLayoutRenderer : AspNetLayoutRendererBase
{
/// <summary>
/// Initializes a new instance of the <see cref="AspNetSessionValueLayoutRenderer" /> class.
/// </summary>
public AspNetSessionValueLayoutRenderer()
{
this.Culture = CultureInfo.CurrentUICulture;
}

/// <summary>
/// Gets or sets the session variable name.
Expand All @@ -58,6 +65,12 @@ public class AspNetSessionValueLayoutRenderer : AspNetLayoutRendererBase
/// <docgen category='Rendering Options' order='10' />
public bool EvaluateAsNestedProperties { get; set; }

/// <summary>
/// Gets or sets the culture used for rendering.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public CultureInfo Culture { get; set; }

/// <summary>
/// Renders the specified ASP.NET Session value and appends it to the specified <see cref="StringBuilder" />.
/// </summary>
Expand Down Expand Up @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions NLog.Web.AspNetCore/NLog.Web.AspNetCore.xproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>74d5915b-bea9-404c-b4d0-b663164def37</ProjectGuid>
<RootNamespace>NLog.Web</RootNamespace>
Expand All @@ -18,5 +18,5 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<ProduceOutputsOnBuild>True</ProduceOutputsOnBuild>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
File renamed without changes.

0 comments on commit 2ab8049

Please sign in to comment.