Skip to content

Commit

Permalink
Add missing space padding before exception message in log (dotnet#39534
Browse files Browse the repository at this point in the history
  • Loading branch information
maryamariyan authored Jul 22, 2020
1 parent ffe0ab1 commit 847f2fa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ private void CreateDefaultLogMessage<TState>(TextWriter textWriter, in LogEntry<

// scope information
WriteScopeInformation(textWriter, scopeProvider, singleLine);
if (singleLine)
{
textWriter.Write(' ');
}
WriteMessage(textWriter, message, singleLine);

// Example:
Expand All @@ -112,6 +108,7 @@ private void WriteMessage(TextWriter textWriter, string message, bool singleLine
{
if (singleLine)
{
textWriter.Write(' ');
WriteReplacing(textWriter, Environment.NewLine, " ", message);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,35 @@ public void Log_SingleLine_LogsWhenMessageIsNotProvided()
GetMessage(sink.Writes.GetRange(1 * t.WritesPerMsg, t.WritesPerMsg)));

Assert.Equal(
"crit: test[0]" + " " + "[null]" + "System.InvalidOperationException: Invalid value" + Environment.NewLine,
"crit: test[0]" + " " + "[null]" + " " + "System.InvalidOperationException: Invalid value" + Environment.NewLine,
GetMessage(sink.Writes.GetRange(2 * t.WritesPerMsg, t.WritesPerMsg)));
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void Log_SingleLine_LogsWhenBothMessageAndExceptionProvided()
{
// Arrange
var t = SetUp(
new ConsoleLoggerOptions { FormatterName = ConsoleFormatterNames.Simple },
new SimpleConsoleFormatterOptions { SingleLine = true }
);
var logger = (ILogger)t.Logger;
var sink = t.Sink;
var exception = new InvalidOperationException("Invalid value");

// Act
logger.LogCritical(eventId: 0, message: "exception happened");
logger.LogCritical(eventId: 0, message: "exception happened", exception: exception);

// Assert
Assert.Equal(4, sink.Writes.Count);
Assert.Equal(
"crit: test[0]" + " " + "exception happened" + Environment.NewLine,
GetMessage(sink.Writes.GetRange(0 * t.WritesPerMsg, t.WritesPerMsg)));

Assert.Equal(
"crit: test[0]" + " " + "exception happened" + " " + "System.InvalidOperationException: Invalid value" + Environment.NewLine,
GetMessage(sink.Writes.GetRange(1 * t.WritesPerMsg, t.WritesPerMsg)));
}
}
}

0 comments on commit 847f2fa

Please sign in to comment.