Skip to content

Commit

Permalink
MicrosoftConsoleJsonLayout - Fixed IndexOutOfRangeException when larg…
Browse files Browse the repository at this point in the history
…e EventID (#571)
  • Loading branch information
snakefoot authored Feb 5, 2022
1 parent cfb772e commit 45ec960
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)
builder.Append(": ");
builder.Append(logEvent.LoggerName);
builder.Append('[');
int eventId = LookupEventId(logEvent);
builder.Append(ConvertEventId(eventId));
var eventId = LookupEventId(logEvent);
builder.Append(eventId);
builder.Append(']');
builder.Append(System.Environment.NewLine);
builder.Append(" ");
Expand All @@ -63,18 +63,25 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)
}
}

private static int LookupEventId(LogEventInfo logEvent)
private static string LookupEventId(LogEventInfo logEvent)
{
int eventId = 0;
if (logEvent.HasProperties && (logEvent.Properties.TryGetValue(nameof(EventIdCaptureType.EventId_Id), out var eventIdValue) || logEvent.Properties.TryGetValue(nameof(EventIdCaptureType.EventId), out eventIdValue)))
if (logEvent.HasProperties)
{
if (eventIdValue is int)
eventId = (int)eventIdValue;
else if (eventIdValue is Microsoft.Extensions.Logging.EventId eventIdStuct)
eventId = eventIdStuct.Id;
if (logEvent.Properties.TryGetValue(nameof(EventIdCaptureType.EventId), out var eventObject))
{
if (eventObject is int eventId)
return ConvertEventId(eventId);
else if (eventObject is Microsoft.Extensions.Logging.EventId eventIdStruct)
return ConvertEventId(eventIdStruct.Id);
}

if (logEvent.Properties.TryGetValue(nameof(EventIdCaptureType.EventId_Id), out var eventid) && eventid is int)
{
return ConvertEventId((int)eventid);
}
}

return eventId;
return "0";
}

private static string ConvertEventId(int eventId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static string ConvertEventId(int eventId)
{
if (eventId == 0)
return "0";
else if (eventId > 0 || eventId < EventIdMapper.Length)
else if (eventId > 0 && eventId < EventIdMapper.Length)
return EventIdMapper[eventId];
else
return eventId.ToString(System.Globalization.CultureInfo.InvariantCulture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public void MicrosoftConsoleJsonLayout_ExceptionEvent()
var logEvent1 = new LogEventInfo(LogLevel.Error, "MyLogger", null, "Alert {EventId}", new object[] { eventId }, exception);
var result1 = layout.Render(logEvent1);
Assert.Equal($"{{ \"Timestamp\": \"{logEvent1.TimeStamp.ToUniversalTime().ToString("O")}\", \"EventId\": {eventId}, \"LogLevel\": \"Error\", \"Category\": \"MyLogger\", \"Message\": \"Alert {eventId}\", \"Exception\": \"{exception.ToString()}\", \"State\": {{ \"{{OriginalFormat}}\": \"Alert {{EventId}}\" }} }}", result1);
var logEvent2 = new LogEventInfo(LogLevel.Error, "MyLogger", null, "Alert {EventId_Id}", new object[] { eventId }, exception);

var eventId2 = 420;
var logEvent2 = new LogEventInfo(LogLevel.Error, "MyLogger", null, "Alert {EventId_Id}", new object[] { eventId2 }, exception);
var result2 = layout.Render(logEvent2);
Assert.Equal($"{{ \"Timestamp\": \"{logEvent2.TimeStamp.ToUniversalTime().ToString("O")}\", \"EventId\": {eventId}, \"LogLevel\": \"Error\", \"Category\": \"MyLogger\", \"Message\": \"Alert {eventId}\", \"Exception\": \"{exception.ToString()}\", \"State\": {{ \"{{OriginalFormat}}\": \"Alert {{EventId_Id}}\" }} }}", result2);
Assert.Equal($"{{ \"Timestamp\": \"{logEvent2.TimeStamp.ToUniversalTime().ToString("O")}\", \"EventId\": {eventId2}, \"LogLevel\": \"Error\", \"Category\": \"MyLogger\", \"Message\": \"Alert {eventId2}\", \"Exception\": \"{exception.ToString()}\", \"State\": {{ \"{{OriginalFormat}}\": \"Alert {{EventId_Id}}\" }} }}", result2);
}

[Fact]
Expand Down

0 comments on commit 45ec960

Please sign in to comment.