Skip to content

Commit

Permalink
Changed format Culture to CultureInfo.InvariantCulture to match defau…
Browse files Browse the repository at this point in the history
…lt NLog Culture (#735)
  • Loading branch information
snakefoot authored Dec 2, 2021
1 parent dbc54b9 commit c1c8a98
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public class AspNetApplicationValueLayoutRenderer : AspNetLayoutRendererBase
[DefaultParameter]
public string Variable { get; set; }

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

/// <summary>
/// Renders the specified ASP.NET Application variable and appends it to the specified <see cref="StringBuilder" />.
/// </summary>
Expand All @@ -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));
}
}
}
10 changes: 1 addition & 9 deletions src/Shared/LayoutRenderers/AspNetItemValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ 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()
{
Culture = CultureInfo.CurrentUICulture;
}

/// <summary>
/// Gets or sets the item variable name.
/// </summary>
Expand All @@ -68,7 +60,7 @@ public AspNetItemValueLayoutRenderer()
/// Gets or sets the culture used for rendering.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public CultureInfo Culture { get; set; }
public CultureInfo Culture { get; set; } = CultureInfo.InvariantCulture;

/// <summary>
/// Renders the specified ASP.NET Item value and appends it to the specified <see cref="StringBuilder" />.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using NLog.Config;
Expand Down Expand Up @@ -31,14 +32,14 @@ public class AspNetRequestDurationLayoutRenderer : AspNetLayoutRendererBase
/// Gets or sets the culture used for rendering.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public System.Globalization.CultureInfo Culture { get; set; } = System.Globalization.CultureInfo.InvariantCulture;
public CultureInfo Culture { get; set; } = CultureInfo.InvariantCulture;

/// <inheritdoc/>
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
Expand All @@ -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}}}");
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ public class AspNetSessionValueLayoutRenderer : AspNetLayoutRendererBase
#if ASP_NET_CORE
private const string NLogRetrievingSessionValue = "NLogRetrievingSessionValue";
#endif
/// <summary>
/// Initializes a new instance of the <see cref="AspNetSessionValueLayoutRenderer" /> class.
/// </summary>
public AspNetSessionValueLayoutRenderer()
{
Culture = CultureInfo.CurrentUICulture;
}

/// <summary>
/// Gets or sets the session variable name.
Expand All @@ -69,7 +62,7 @@ public AspNetSessionValueLayoutRenderer()
/// Gets or sets the culture used for rendering.
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public CultureInfo Culture { get; set; }
public CultureInfo Culture { get; set; } = CultureInfo.InvariantCulture;

#if ASP_NET_CORE
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ public void VariableFoundRendersValue(object expectedValue)
var httpContext = Substitute.For<HttpContextBase>();
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<object[]> VariableFoundData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand All @@ -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<object[]> VariableFoundData
Expand Down

0 comments on commit c1c8a98

Please sign in to comment.