如何通过 WriteEventCore 将 DateTime 存储在 EventSource 中

How to store DateTime in EventSource via WriteEventCore

这是我当前的代码:

[NonEvent]
    unsafe void InsertViaWriteEventCore(
        int eventId,
        DateTime startDateTime,
        DateTime endDateTime)
    {
        const int eventDataCount = 2;
        {
            EventData* data = stackalloc EventData[eventDataCount];
            data->DataPointer = (IntPtr)(&startDateTime);
            data->Size = sizeof(DateTime);
            data[1].DataPointer = (IntPtr)(&endDateTime);
            data[1].Size = sizeof(DateTime);

            WriteEventCore(eventId, eventDataCount, data);
        }
    }

但最后,在生成的事件中,如果我将 startDateTime 提供为“8/30/2017 5:00:00 PM”,我会看到“8/30/3617 5:00:00 PM”作为 startDateTime到方法。年总是加1600

我尝试提供 8 as the size instead of sizeof(DateTime) and i still got the same result. I get the right date values if I change the dates to string and pass to writeeventcore as string as shown in the example here。我在这里做错了什么?通过 WriteEventCore 传递 DateTime 的正确方法是什么?

当您使用“EventData”时,您必须使用 ETW 期望的日期时间。这恰好是 windows FileTime 格式(这是一个 long,即从 1600 开始的秒数)

因此,要将 DateTime 传递给 EventData,您必须执行如下操作。

long startFileTime = startDateTime.ToFileTime();
data->DataPointer = (IntPtr)(& startFileTime);
data->Size = sizeof(long);