Skip to content

Commit

Permalink
MicrosoftConsoleLayoutRenderer - Skip string-allocation for EventId w…
Browse files Browse the repository at this point in the history
…hen possible on platform (#572)
  • Loading branch information
snakefoot committed Jun 4, 2022
1 parent 48e4c77 commit d5af5fc
Showing 1 changed file with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,9 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)
builder.Append(microsoftLogLevel);
builder.Append(": ");
builder.Append(logEvent.LoggerName);
builder.Append("[");
int eventId = 0;
if (logEvent.HasProperties && logEvent.Properties.TryGetValue("EventId_Id", out var eventIdValue))
{
if (eventIdValue is int)
eventId = (int)eventIdValue;
else if (!int.TryParse(eventIdValue?.ToString() ?? string.Empty, out eventId))
eventId = 0;
}
else
{
eventId = 0;
}
builder.Append(ConvertEventId(eventId));
builder.Append("]");
builder.Append('[');
AppendEventId(LookupEventId(logEvent), builder);
builder.Append(']');
builder.Append(System.Environment.NewLine);
builder.Append(" ");
builder.Append(logEvent.FormattedMessage);
Expand All @@ -47,14 +35,38 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)
}
}

static string ConvertEventId(int eventId)
private static void AppendEventId(int eventId, StringBuilder builder)
{
if (eventId == 0)
return "0";
builder.Append('0');
else if (eventId > 0 && eventId < EventIdMapper.Length)
return EventIdMapper[eventId];
builder.Append(EventIdMapper[eventId]);
else
return eventId.ToString();
builder.Append(eventId); // .NET5 (and newer) can append integer without string-allocation (using span)
}

private static int LookupEventId(LogEventInfo logEvent)
{
if (logEvent.HasProperties)
{
if (logEvent.Properties.TryGetValue("EventId_Id", out var eventObject))
{
if (eventObject is int eventId)
return eventId;
else if (eventObject is Microsoft.Extensions.Logging.EventId eventIdStruct)
return eventIdStruct.Id;
}

if (logEvent.Properties.TryGetValue("EventId", out var eventid))
{
if (eventObject is int eventId)
return eventId;
else if (eventObject is Microsoft.Extensions.Logging.EventId eventIdStruct)
return eventIdStruct.Id;
}
}

return 0;
}

string ConvertLogLevel(LogLevel logLevel)
Expand Down

0 comments on commit d5af5fc

Please sign in to comment.