Skip to content

Commit

Permalink
Avoid use of FormattableString when logging (#5503)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Oct 11, 2024
1 parent 85e70b0 commit dbab257
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ private void GenLogMethod(LoggingMethod lm)
});

var s = EscapeMessageString(mapped!);
OutLn($@"#if NET");
OutLn($@"return string.Create(global::System.Globalization.CultureInfo.InvariantCulture, ${s});");
OutLn($@"#else");
OutLn($@"return global::System.FormattableString.Invariant(${s});");
OutLn($@"#endif");
}
else if (string.IsNullOrEmpty(lm.Message))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

using System;
using System.Diagnostics.CodeAnalysis;
#if NET
using System.Globalization;
#endif
using System.Net.Http;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -152,7 +155,11 @@ private static string OriginalFormatValueFmt(LoggerMessageState request, Excepti
var httpMethod = request[startIndex].Value;
var httpHost = request[startIndex + 1].Value;
var httpPath = request[startIndex + 2].Value;
#if NET
return string.Create(CultureInfo.InvariantCulture, stackalloc char[256], $"{httpMethod} {httpHost}/{httpPath}");
#else
return FormattableString.Invariant($"{httpMethod} {httpHost}/{httpPath}");
#endif
}

private static int FindStartIndex(LoggerMessageState request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
#if NET
using System.Globalization;
#endif
using Microsoft.Shared.Pools;

namespace Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -67,7 +70,11 @@ public static string Stringify(IEnumerable? enumerable)
}
else
{
#if NET
_ = sb.Append(CultureInfo.InvariantCulture, $"\"{e}\"");
#else
_ = sb.Append(FormattableString.Invariant($"\"{e}\""));
#endif
}

first = false;
Expand Down Expand Up @@ -108,7 +115,11 @@ public static string Stringify<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TVal

if (typeof(TKey).IsValueType || kvp.Key is not null)
{
#if NET
_ = sb.Append(CultureInfo.InvariantCulture, $"\"{kvp.Key}\"=");
#else
_ = sb.Append(FormattableString.Invariant($"\"{kvp.Key}\"="));
#endif
}
else
{
Expand All @@ -117,7 +128,11 @@ public static string Stringify<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TVal

if (typeof(TValue).IsValueType || kvp.Value is not null)
{
#if NET
_ = sb.Append(CultureInfo.InvariantCulture, $"\"{kvp.Value}\"");
#else
_ = sb.Append(FormattableString.Invariant($"\"{kvp.Value}\""));
#endif
}
else
{
Expand Down

0 comments on commit dbab257

Please sign in to comment.