EventSource.WriteEventCore(Int32, Int32, EventSource+EventData*) 方法



此 API 不符合 CLS。

通过使用提供的事件标识符和事件数据,创建新的 WriteEvent 重载。Creates a new WriteEvent overload by using the provided event identifier and event data.

 void WriteEventCore(int eventId, int eventDataCount, System::Diagnostics::Tracing::EventSource::EventData* data);
protected void WriteEventCore (int eventId, int eventDataCount, System.Diagnostics.Tracing.EventSource.EventData* data);
protected void WriteEventCore (int eventId, int eventDataCount, System.Diagnostics.Tracing.EventSource.EventData* data);
member this.WriteEventCore : int * int * nativeptr<System.Diagnostics.Tracing.EventSource.EventData> -> unit
member this.WriteEventCore : int * int * nativeptr<System.Diagnostics.Tracing.EventSource.EventData> -> unit



事件标识符。The event identifier.


事件数据项的数目。The number of event data items.


包含事件数据的结构。The structure that contains the event data.



eventid 应大于0或小于65535,否则操作中可能会发生错误。eventid should be greater than 0 or less than 65535 or errors can occur in the operation. 如果发生错误,则可以通过检查调试器的输出流来获取有关错误来源的详细信息,前提是已将调试器附加到进程触发事件。If errors do occur, you can get more information about the source of the error by checking the output stream of the debugger, if you have a debugger attached to the process firing events. 如果在发生错误的事件源上有 ETW 侦听器,还可以查看 ETW 事件流中报告的错误。You can also look for errors reported in the ETW event stream, if you have an ETW listener on the event source where the error occurs.

此受保护的方法使用户能够定义 WriteEvent 比提供的重载更快的新重载。This protected method enables users to define new WriteEvent overloads that are faster than the provided overloads. 创建新的重载涉及不安全代码。Creating a new overload involves unsafe code. 基本过程是对与有效负载项数匹配的事件数据描述符数组进行堆栈分配。The basic procedure is to stack-allocate an array of event data descriptors that matches the number of payload items. 对于每个负载项,请在事件数据数组中设置正确的大小和值。调用已 WriteEventCore 初始化的数组。For each payload item, set the correct size and value in the event data array.Call WriteEventCore with the initialized array.

下面的示例演示如何添加 WriteEvent 接受四个自变量的重载。The following example shows how to add a WriteEvent overload that accepts four arguments. 例如,如果您有记录一个字符串和3个整数的日志记录事件,就会出现这种情况。An example would be if you have a logging event that logs a string and 3 integers.

public void LogTime(string tag, int hour, int minute, int second)   
    WriteEvent(1, tag, hour, minute, second);  

你可以在不调用的情况下执行此操作 WriteEventCore ,但它比需要的速度要慢。You could do this without calling WriteEventCore, but it would be slower than it needs to be. 这是因为它使用数组和反射来确定要执行的操作。That is because it uses arrays and reflection to figure out what to do. 如果以高速率记录这些 (> 1000/秒) ,则可能需要创建快速帮助器,如以下示例中所示。If you log these at a high rate (> 1000 / sec), it can be worth making a fast helper, as shown in the following example. 方法隐藏现有的 WriteEventThe method shadows the existing WriteEvent. 因此,原始调用方代码 (LogTime) 并不真正改变,但 c # 编译器将使用更快的更专业化的版本。Thus the original caller code (LogTime) does not actually change, but the C# compiler will use the more specialized version which will be faster.

若要编译不安全代码,必须) 编译器选项指定 /unsafe (c # 编译器选项 To compile unsafe code, you must specify the /unsafe (C# Compiler Options) compiler option.

class AnotherEventSource : EventSource {  

    public unsafe void WriteEvent(int eventId, string arg1, int arg2, int arg3, int arg4)  

        fixed (char* arg1Ptr = arg1)  
            EventData* dataDesc = stackalloc EventData[4];  

            dataDesc[0].DataPointer = (IntPtr)arg1Ptr;  
            dataDesc[0].Size = (arg1.Length + 1) * 2; // Size in bytes, including a null terminator.   
            dataDesc[1].DataPointer = (IntPtr)(&arg2);  
            dataDesc[1].Size = 4;  
            dataDesc[2].DataPointer = (IntPtr)(&arg3);  
            dataDesc[2].Size = 4;  
            dataDesc[3].DataPointer = (IntPtr)(&arg4);  
            dataDesc[3].Size = 4;  

            WriteEventCore(eventId, 4, dataDesc);