EventSource.WriteEventCore Method (Int32, Int32, EventSource.EventData*)

.NET Framework (current version)

The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Creates a new WriteEvent overload by using the provided event identifier and event data.

This API is not CLS-compliant.

Namespace:   System.Diagnostics.Tracing
Assembly:  mscorlib (in mscorlib.dll)

protected unsafe void WriteEventCore(
	int eventId,
	int eventDataCount,
	EventSource.EventData* data


Type: System.Int32

The event identifier.

Type: System.Int32

The number of event data items.

Type: System.Diagnostics.Tracing.EventSource.EventData*

The structure that contains the event data.

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. 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.

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. For each payload item, set the correct size and value in the event data array.Call WriteEventCore with the initialized array.

The following example shows how to add a WriteEvent overload that accepts four arguments. 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);

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. If you log these at a high rate (> 1000 / sec), it can be worth making a fast helper, as shown in the following example. The method shadows the existing WriteEvent. Thus the original caller code (LogTime) does not actually change, but the C# compiler will use the more specialized version which will be faster.

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 EventProvider.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, (IntPtr)dataDesc);


requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
Return to top