Skip to content

Commit

Permalink
JsonConsoleFormatter: keep escaped line breaks for Exceptions #83726 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
daberni authored Apr 20, 2023
1 parent c27a160 commit 02ddff3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ public override void Write<TState>(in LogEntry<TState> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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));
}
}
}

0 comments on commit 02ddff3

Please sign in to comment.