From 02ddff3430d976a7cb3a785b1ec7e33e80796e71 Mon Sep 17 00:00:00 2001 From: Bernhard Danecker Date: Fri, 21 Apr 2023 01:38:02 +0200 Subject: [PATCH] JsonConsoleFormatter: keep escaped line breaks for Exceptions #83726 (#84972) --- .../src/JsonConsoleFormatter.cs | 7 +------ .../JsonConsoleFormatterTests.cs | 18 +++++++++--------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs index e3e2aa185ade8..122556bbbe460 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/JsonConsoleFormatter.cs @@ -55,12 +55,7 @@ public override void Write(in LogEntry logEntry, IExternalScopeP if (exception != null) { - string exceptionMessage = exception.ToString(); - if (!FormatterOptions.JsonWriterOptions.Indented) - { - exceptionMessage = exceptionMessage.Replace(Environment.NewLine, " "); - } - writer.WriteString(nameof(Exception), exceptionMessage); + writer.WriteString(nameof(Exception), exception.ToString()); } if (logEntry.State != null) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/JsonConsoleFormatterTests.cs b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/JsonConsoleFormatterTests.cs index 37122859b1e67..0ef491abbc172 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/JsonConsoleFormatterTests.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/JsonConsoleFormatterTests.cs @@ -490,7 +490,7 @@ private string GetJson(Exception exception, bool indented) JsonConsoleFormatterOptions jsonOptions = new JsonConsoleFormatterOptions() { JsonWriterOptions = new JsonWriterOptions() - { + { Indented = indented, Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping } @@ -519,15 +519,15 @@ public void ShouldContainInnerException(bool indented) Assert.Contains(rootException.Message, json); Assert.Contains(rootException.InnerException.Message, json); - - Assert.Contains(GetContent(rootException, indented), json); - Assert.Contains(GetContent(rootException.InnerException, indented), json); + + Assert.Contains(GetContent(rootException), json); + Assert.Contains(GetContent(rootException.InnerException), json); } - static string GetContent(Exception exception, bool indented) + static string GetContent(Exception exception) { // Depending on OS, Environment.NewLine is either '\r\n' OR '\n' - string newLineReplacement = indented ? (Environment.NewLine.Length == 2 ? "\\r\\n" : "\\n") : " "; + string newLineReplacement = Environment.NewLine.Length == 2 ? "\\r\\n" : "\\n"; return exception.ToString() .Replace(@"\", @"\\") // for paths in json content @@ -546,9 +546,9 @@ public void ShouldContainAggregateExceptions(bool indented) Assert.Contains(rootException.Message, json); rootException.InnerExceptions.ToList().ForEach((inner) => Assert.Contains(inner.Message, json)); - - Assert.Contains(GetContent(rootException, indented), json); - rootException.InnerExceptions.ToList().ForEach((inner) => Assert.Contains(GetContent(inner, indented), json)); + + Assert.Contains(GetContent(rootException), json); + rootException.InnerExceptions.ToList().ForEach((inner) => Assert.Contains(GetContent(inner), json)); } } }