Handle log messages with null state (#4319)
authorJoe Schmitt <1146681+schmittjoseph@users.noreply.github.com>
Thu, 12 Oct 2023 22:25:15 +0000 (15:25 -0700)
committerGitHub <noreply@github.com>
Thu, 12 Oct 2023 22:25:15 +0000 (15:25 -0700)
This PR addresses an issue were certain log messages could be dropped:
- A log message with placeholders can have an entirely null state (e.g.
`_logger.LogInformational("Test {A}", args: null)`). In this scenario,
the event source args payload will only contain the original format
string, not the null args.
- The current code assumes all placeholders will exist in the event
source args payload, which results in an exception being thrown and the
log message being dropped.

src/Microsoft.Diagnostics.Monitoring.EventPipe/Logs/EventLogsPipeline.cs

index eec6facb481cbcc996d6ea7f526e4981e46b5aa1..063bd342fbdc3c7b732f225c15609f0d361219a2 100644 (file)
@@ -137,7 +137,10 @@ namespace Microsoft.Diagnostics.Monitoring.EventPipe
                         object[] args = new object[formatter.ValueNames.Count];
                         for (int i = 0; i < args.Length; i++)
                         {
-                            args[i] = message.GetProperty(formatter.ValueNames[i]).GetString();
+                            if (message.TryGetProperty(formatter.ValueNames[i], out JsonElement value))
+                            {
+                                args[i] = value.GetString();
+                            }
                         }
 
                         //We want to propagate the timestamp to the underlying logger, but that's not part of the ILogger interface.