自从我们升级到任何高于 4.5.2 的 .NET Framework 版本后,我就遇到了这个问题。我终于弄清楚了我的问题,希望这对你也有帮助。我知道这是一篇旧帖子,但也许它也会对其他人有所帮助。
Windows 事件跟踪 (ETW) 似乎可以处理错误,而您获得的唯一指示就是您看到的记录消息为空的位置。我正在使用从 IEventTextFormatter 派生的自定义格式化程序。 (EventData.FormattedMessage 为空)。在此方法中设置断点显示 EventData.FormattedMessage 为空。我还在 Visual Studio 的输出/调试窗口中注意到以下消息:
The parameters to the Event method do not match the parameters to the WriteEvent method. This may cause the event to be displayed incorrectly.
EventSourceException while processing event "WriteLogDefault": System.IndexOutOfRangeException:Index was outside the bounds of the array.
A first chance exception of type 'System.NullReferenceException' occurred in NEAM.App.ProfileDataMart.BL.dll
我将我的问题追溯到以下方法:
[Event(1, Message = "{2}", Level = EventLevel.Informational, Keywords = Keywords.Informational)]
private void WriteLogDefault(int p_EventId, string p_AssemblyName, int p_Key, string p_Message)
{
WriteEvent(p_EventId, p_AssemblyName, p_Key, p_Message);
}
根据MSDN 上的文档中的注释,对WriteEvent 的调用应包括EventId,后跟传递给您的方法的所有参数。显然,上述代码在 .NET Framework 4.5.2 或更早版本中没有引发任何异常。
当您在 EventSource 派生类中实现被标识为 ETW 事件的方法时。您必须调用基类 WriteEvent 方法,传递 EventId 和与实现方法相同的参数。
我的代码现在看起来像这样并且可以按预期工作。祝你好运!
[Event(1, Message = "{2}", Level = EventLevel.Informational, Keywords = Keywords.Informational)]
public void WriteLogDefault(string p_AssemblyName, int p_Key, string p_Message)
{
WriteEvent(1, p_AssemblyName, p_Key, p_Message);
}