From c9b703d23a963e9ed27307a0e3559e6210163bed Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 16 Nov 2020 18:06:33 -0500 Subject: [PATCH] Avoid string.Concat(params string[]) in SimpleConsoleFormatter.CreateDefaultLogMessage It's resulting in a string[] allocation and a string allocation, when we can instead just make a few more individual calls to Write and stackalloc the integer. --- .../src/SimpleConsoleFormatter.cs | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatter.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatter.cs index bc702ca0438ad..1c790a45e730d 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatter.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatter.cs @@ -3,10 +3,6 @@ using System; using System.IO; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; @@ -78,7 +74,19 @@ private void CreateDefaultLogMessage(TextWriter textWriter, in LogEntry< // Request received // category and event id - textWriter.Write(LoglevelPadding + logEntry.Category + '[' + eventId + "]"); + textWriter.Write(LoglevelPadding); + textWriter.Write(logEntry.Category); + textWriter.Write('['); + +#if NETCOREAPP + Span span = stackalloc char[10]; + if (eventId.TryFormat(span, out int charsWritten)) + textWriter.Write(span.Slice(0, charsWritten)); + else +#endif + textWriter.Write(eventId.ToString()); + + textWriter.Write(']'); if (!singleLine) { textWriter.Write(Environment.NewLine); @@ -177,7 +185,8 @@ private void WriteScopeInformation(TextWriter textWriter, IExternalScopeProvider if (paddingNeeded) { paddingNeeded = false; - state.Write(_messagePadding + "=> "); + state.Write(_messagePadding); + state.Write("=> "); } else {