Skip to content

Commit

Permalink
Avoid string.Concat(params string[]) in SimpleConsoleFormatter.Create…
Browse files Browse the repository at this point in the history
…DefaultLogMessage (#44765)

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.
  • Loading branch information
stephentoub authored Nov 18, 2020
1 parent 2d80101 commit 38259c8
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -78,7 +74,19 @@ private void CreateDefaultLogMessage<TState>(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<char> 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);
Expand Down Expand Up @@ -177,7 +185,8 @@ private void WriteScopeInformation(TextWriter textWriter, IExternalScopeProvider
if (paddingNeeded)
{
paddingNeeded = false;
state.Write(_messagePadding + "=> ");
state.Write(_messagePadding);
state.Write("=> ");
}
else
{
Expand Down

0 comments on commit 38259c8

Please sign in to comment.