From 45ec96037a2072f89567c08b2592277751846fd7 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sat, 5 Feb 2022 12:12:33 +0100 Subject: [PATCH] MicrosoftConsoleJsonLayout - Fixed IndexOutOfRangeException when large EventID (#571) --- .../MicrosoftConsoleLayoutRenderer.cs | 27 ++++++++++++------- .../Layouts/MicrosoftConsoleJsonLayout.cs | 2 +- .../MicrosoftConsoleJsonLayoutTests.cs | 6 +++-- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/NLog.Extensions.Logging/LayoutRenderers/MicrosoftConsoleLayoutRenderer.cs b/src/NLog.Extensions.Logging/LayoutRenderers/MicrosoftConsoleLayoutRenderer.cs index 1ba0db41..bc159045 100644 --- a/src/NLog.Extensions.Logging/LayoutRenderers/MicrosoftConsoleLayoutRenderer.cs +++ b/src/NLog.Extensions.Logging/LayoutRenderers/MicrosoftConsoleLayoutRenderer.cs @@ -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(" "); @@ -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) diff --git a/src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs b/src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs index 1d167d3a..cb30e830 100644 --- a/src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs +++ b/src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs @@ -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); diff --git a/test/NLog.Extensions.Logging.Tests/MicrosoftConsoleJsonLayoutTests.cs b/test/NLog.Extensions.Logging.Tests/MicrosoftConsoleJsonLayoutTests.cs index 6b9fdd07..aef6af55 100644 --- a/test/NLog.Extensions.Logging.Tests/MicrosoftConsoleJsonLayoutTests.cs +++ b/test/NLog.Extensions.Logging.Tests/MicrosoftConsoleJsonLayoutTests.cs @@ -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]