Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Culture option to ${aspnet-item} and {aspnet-session} #123

Merged
merged 6 commits into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.