From dbab2572a5cee1dd5624fbd3197df65a5b14b2f8 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Fri, 11 Oct 2024 08:57:07 -0400 Subject: [PATCH] Avoid use of FormattableString when logging (#5503) --- .../Emission/Emitter.Method.cs | 4 ++++ .../Logging/Internal/Log.cs | 7 +++++++ .../Logging/LoggerMessageHelper.cs | 15 +++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/src/Generators/Microsoft.Gen.Logging/Emission/Emitter.Method.cs b/src/Generators/Microsoft.Gen.Logging/Emission/Emitter.Method.cs index a7fa6b02e59..8b0f4e98727 100644 --- a/src/Generators/Microsoft.Gen.Logging/Emission/Emitter.Method.cs +++ b/src/Generators/Microsoft.Gen.Logging/Emission/Emitter.Method.cs @@ -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)) { diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/Log.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/Log.cs index ea60895faf9..c156eb72419 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/Log.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/Log.cs @@ -3,6 +3,9 @@ using System; using System.Diagnostics.CodeAnalysis; +#if NET +using System.Globalization; +#endif using System.Net.Http; using Microsoft.Extensions.Logging; @@ -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) diff --git a/src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LoggerMessageHelper.cs b/src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LoggerMessageHelper.cs index f74b4dac85c..bb3631909dc 100644 --- a/src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LoggerMessageHelper.cs +++ b/src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Logging/LoggerMessageHelper.cs @@ -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; @@ -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; @@ -108,7 +115,11 @@ public static string Stringify(IEnumerable