[release/6.0] Fix in-process decoding of DateTime in EventSource (#68352)
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Wed, 4 May 2022 19:37:54 +0000 (12:37 -0700)
committerGitHub <noreply@github.com>
Wed, 4 May 2022 19:37:54 +0000 (12:37 -0700)
* Fix in-process decoding of DateTime in EventSource

* Update eventlistener test to validate DateTime is correctly decoded in-process

* Use UTC time everywhere

Co-authored-by: John Salem <josalem@microsoft.com>
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs
src/tests/tracing/eventlistener/EventListener.cs

index f8d6e9c6ff114d093a1051472cce9b459c9e6e7b..cdaa7eea9c1935c4bf2b7157479cfde7e7c9acf4 100644 (file)
@@ -1839,7 +1839,7 @@ namespace System.Diagnostics.Tracing
                         }
                         else if (typeCode == TypeCode.DateTime)
                         {
-                            decoded = *(DateTime*)dataPointer;
+                            decoded = DateTime.FromFileTimeUtc(*(long*)dataPointer);
                         }
                         else if (IntPtr.Size == 8 && dataType == typeof(IntPtr))
                         {
index 23a1ff37d131b782a011ce0d3d3bff6cdae17f76..0cd7e18459cb82b7e8a62105ce735fbeb060623e 100644 (file)
@@ -15,6 +15,20 @@ namespace Tracing.Tests
 
         [Event(1)]
         internal void MathResult(int x, int y, int z, string formula) { this.WriteEvent(1, x, y, z, formula); }
+
+        [Event(2)]
+        internal void DateTimeEvent(DateTime dateTime) => WriteEvent(2, dateTime);
+
+
+        [NonEvent]  
+        private unsafe void WriteEvent(int eventId, DateTime dateTime)  
+        {
+            EventData* desc = stackalloc EventData[1];
+            long fileTime = dateTime.ToFileTimeUtc();
+            desc[0].DataPointer = (IntPtr)(&fileTime);
+            desc[0].Size = 8;
+            WriteEventCore(eventId, 1, desc);
+        }
     }
     
     internal sealed class SimpleEventListener : EventListener
@@ -23,6 +37,7 @@ namespace Tracing.Tests
         private readonly EventLevel _level;
         
         public int EventCount { get; private set; } = 0;
+        public DateTime DateObserved { get; private set; } = DateTime.MinValue;
 
         public SimpleEventListener(string targetSourceName, EventLevel level)
         {
@@ -41,13 +56,19 @@ namespace Tracing.Tests
 
         protected override void OnEventWritten(EventWrittenEventArgs eventData)
         {
-            EventCount++;
+            if (eventData.EventId == 2)
+            {
+                DateObserved = (DateTime)eventData.Payload[0];
+            }
+            else
+                EventCount++;
         }
     }
 
     class EventPipeSmoke
     {
         private static int messageIterations = 100;
+        private static readonly DateTime ThePast = DateTime.UtcNow;
 
         static int Main(string[] args)
         {
@@ -68,10 +89,12 @@ namespace Tracing.Tests
                     
                     eventSource.MathResult(x, y, x+y, formula);
                 }
+                eventSource.DateTimeEvent(ThePast);
                 Console.WriteLine("\tEnd: Messaging.\n");
                 
                 Console.WriteLine($"\tEventListener received {listener.EventCount} event(s)\n");
-                pass = listener.EventCount == messageIterations;
+                Console.WriteLine($"\tEventListener received {listener.DateObserved} vs {ThePast}\n");
+                pass = listener.EventCount == messageIterations && ThePast == listener.DateObserved;
             }
 
             return pass ? 100 : -1;