[EventSourceLogger] Adding FormattedMessage for json message (#38509)
authorMaryam Ariyan <maryam.ariyan@microsoft.com>
Tue, 30 Jun 2020 23:31:59 +0000 (16:31 -0700)
committerGitHub <noreply@github.com>
Tue, 30 Jun 2020 23:31:59 +0000 (16:31 -0700)
src/libraries/Microsoft.Extensions.Logging.EventSource/src/EventSourceLogger.cs
src/libraries/Microsoft.Extensions.Logging.EventSource/src/LoggingEventSource.cs
src/libraries/Microsoft.Extensions.Logging.EventSource/tests/EventSourceLoggerTest.cs

index 4abbe92..c2712ad 100644 (file)
@@ -57,11 +57,12 @@ namespace Microsoft.Extensions.Logging.EventSource
             {
                 return;
             }
+            string message = null;
 
             // See if they want the formatted message
             if (_eventSource.IsEnabled(EventLevel.Critical, LoggingEventSource.Keywords.FormattedMessage))
             {
-                string message = formatter(state, exception);
+                message = formatter(state, exception);
                 _eventSource.FormattedMessage(
                     logLevel,
                     _factoryID,
@@ -104,6 +105,7 @@ namespace Microsoft.Extensions.Logging.EventSource
                     exceptionJson = ToJson(exceptionInfoData);
                 }
                 IReadOnlyList<KeyValuePair<string, string>> arguments = GetProperties(state);
+                message ??= formatter(state, exception);
                 _eventSource.MessageJson(
                     logLevel,
                     _factoryID,
@@ -111,7 +113,8 @@ namespace Microsoft.Extensions.Logging.EventSource
                     eventId.Id,
                     eventId.Name,
                     exceptionJson,
-                    ToJson(arguments));
+                    ToJson(arguments),
+                    message);
             }
         }
 
index 59ed09e..c6d1730 100644 (file)
@@ -196,7 +196,7 @@ namespace Microsoft.Extensions.Logging.EventSource
         }
 
         [Event(5, Keywords = Keywords.JsonMessage, Level = EventLevel.LogAlways)]
-        internal unsafe void MessageJson(LogLevel Level, int FactoryID, string LoggerName, int EventId, string EventName, string ExceptionJson, string ArgumentsJson)
+        internal unsafe void MessageJson(LogLevel Level, int FactoryID, string LoggerName, int EventId, string EventName, string ExceptionJson, string ArgumentsJson, string FormattedMessage)
         {
             if (IsEnabled())
             {
@@ -204,13 +204,15 @@ namespace Microsoft.Extensions.Logging.EventSource
                 EventName ??= "";
                 ExceptionJson ??= "";
                 ArgumentsJson ??= "";
+                FormattedMessage ??= "";
 
                 fixed (char* loggerName = LoggerName)
                 fixed (char* eventName = EventName)
                 fixed (char* exceptionJson = ExceptionJson)
                 fixed (char* argumentsJson = ArgumentsJson)
+                fixed (char* formattedMessage = FormattedMessage)
                 {
-                    const int eventDataCount = 7;
+                    const int eventDataCount = 8;
                     EventData* eventData = stackalloc EventData[eventDataCount];
 
                     SetEventData(ref eventData[0], ref Level);
@@ -220,6 +222,7 @@ namespace Microsoft.Extensions.Logging.EventSource
                     SetEventData(ref eventData[4], ref EventName, eventName);
                     SetEventData(ref eventData[5], ref ExceptionJson, exceptionJson);
                     SetEventData(ref eventData[6], ref ArgumentsJson, argumentsJson);
+                    SetEventData(ref eventData[7], ref FormattedMessage, formattedMessage);
 
                     WriteEventCore(5, eventDataCount, eventData);
                 }
index 60ad97f..176c8e2 100644 (file)
@@ -444,7 +444,8 @@ namespace Microsoft.Extensions.Logging.Test
 
                 LogStuff(factory);
 
-                var containsNullEventName = false;
+                bool containsNullEventName = false;
+                bool containsFormattedMessage = false;
 
                 foreach (var eventJson in testListener.Events)
                 {
@@ -452,9 +453,14 @@ namespace Microsoft.Extensions.Logging.Test
                     {
                         containsNullEventName = true;
                     }
+                    if (eventJson.Contains(@"""FormattedMessage"":""Logger1 Event1 Debug 1"""))
+                    {
+                        containsFormattedMessage = true;
+                    }
                 }
 
                 Assert.True(containsNullEventName, "EventName is supposed to be null but it isn't.");
+                Assert.True(containsFormattedMessage, "FormattedMessage is supposed to be present but it isn't.");
             }
         }
 
@@ -472,19 +478,25 @@ namespace Microsoft.Extensions.Logging.Test
                 // Write some MessageJson events with null string.
                 for (var i = 0; i < 100; i++)
                 {
-                    LoggingEventSource.Instance.MessageJson(LogLevel.Trace, 1, "MyLogger", 5, null, null, "testJson");
+                    LoggingEventSource.Instance.MessageJson(LogLevel.Trace, 1, "MyLogger", 5, null, null, "testJson", "formattedMessage");
                 }
 
                 bool containsNullEventName = false;
+                bool containsFormattedMessage = false;
                 foreach (var eventJson in testListener.Events)
                 {
                     if (eventJson.Contains(@"""__EVENT_NAME"":""MessageJson""") && eventJson.Contains(@"""EventName"":"""","))
                     {
                         containsNullEventName = true;
                     }
+                    if (eventJson.Contains(@"""FormattedMessage"":""formattedMessage"""))
+                    {
+                        containsFormattedMessage = true;
+                    }
                 }
 
                 Assert.True(containsNullEventName, "EventName and ExceptionJson is supposed to be null but it isn't.");
+                Assert.True(containsFormattedMessage, "FormattedMessage is supposed to be present but it isn't.");
             }
         }