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



事件标识符。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. 对于每个负载项,请在事件数据数组中设置正确的大小和值。调用具有初始化数组的 WriteEventCoreFor 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);