Skip to content

Commit

Permalink
[release/6.0] Fix in-process decoding of DateTime in EventSource (dot…
Browse files Browse the repository at this point in the history
…net#68352)

* 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>
  • Loading branch information
github-actions[bot] and John Salem authored May 4, 2022
1 parent ac796cb commit 1c6bd4d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,7 @@ private static unsafe void DecodeObjects(object?[] decodedObjects, Type[] parame
}
else if (typeCode == TypeCode.DateTime)
{
decoded = *(DateTime*)dataPointer;
decoded = DateTime.FromFileTimeUtc(*(long*)dataPointer);
}
else if (IntPtr.Size == 8 && dataType == typeof(IntPtr))
{
Expand Down
27 changes: 25 additions & 2 deletions src/tests/tracing/eventlistener/EventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ public SimpleEventSource() : base(true) { }

[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
Expand All @@ -23,6 +37,7 @@ internal sealed class SimpleEventListener : EventListener
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)
{
Expand All @@ -41,13 +56,19 @@ protected override void OnEventSourceCreated(EventSource source)

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)
{
Expand All @@ -68,10 +89,12 @@ static int Main(string[] args)

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;
Expand Down

0 comments on commit 1c6bd4d

Please sign in to comment.