Skip to content
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 @@ -66,7 +66,7 @@ public LogValuesFormatter(string format)
formatDelimiterIndex = formatDelimiterIndex < 0 ? closeBraceIndex : formatDelimiterIndex + openBraceIndex;

vsb.Append(format.AsSpan(scanIndex, openBraceIndex - scanIndex + 1));
vsb.Append(_valueNames.Count.ToString());
vsb.Append(_valueNames.Count.ToString(CultureInfo.InvariantCulture));
_valueNames.Add(format.Substring(openBraceIndex + 1, formatDelimiterIndex - openBraceIndex - 1));
vsb.Append(format.AsSpan(formatDelimiterIndex, closeBraceIndex - formatDelimiterIndex + 1));

Expand Down Expand Up @@ -270,7 +270,10 @@ private static bool TryFormatArgumentIfNullOrEnumerable<T>(T? value, [NotNullWhe
vsb.Append(", ");
}

vsb.Append(e != null ? e.ToString() : NullValue);
vsb.Append(
e is IFormattable f ? f.ToString(null, CultureInfo.InvariantCulture) :
e is not null ? e.ToString() :
NullValue);
first = false;
}
stringValue = vsb.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Tests;
using Microsoft.Extensions.Logging.Testing;
using Xunit;

Expand Down Expand Up @@ -134,6 +135,28 @@ public void FormatMessage_LogsCorrectValues()
Assert.Null(debug.Exception);
}

[Fact]
public void FormatMessage_UsesInvariantCulture()
{
// Arrange
using ThreadCultureChange _ = new("fr-FR");
var sink = new TestSink();
var logger = SetUp(sink);

logger.Log(LogLevel.Trace, "{0}", new object[] { 1.23 });
logger.Log(LogLevel.Information, "{0}", new object[] { new object[] { 1.23f } });

Assert.Equal(2, sink.Writes.Count());

Assert.True(sink.Writes.TryTake(out var trace));
Assert.Equal(LogLevel.Trace, trace.LogLevel);
Assert.Equal("1.23", trace.State?.ToString());

Assert.True(sink.Writes.TryTake(out var info));
Assert.Equal(LogLevel.Information, info.LogLevel);
Assert.Equal("1.23", info.State?.ToString());
}

[Fact]
public void MessageAndEventId_LogsCorrectValues()
{
Expand Down
Loading