public bool WriteTransferEvent(ref EventDescriptor eventDescriptor, EventTraceActivity eventTraceActivity, Guid relatedActivityId, params object[] eventPayload)
        {
            // ActivityId is required when writing transfer event
            if (eventTraceActivity == null)
            {
                Fx.Assert(false, "eventTraceActivity should not be null for WriteTransferEvent");
                eventTraceActivity = EventTraceActivity.Empty;
            }

            uint status = 0;

            if (IsEnabled(eventDescriptor.Level, eventDescriptor.Keywords))
            {
                unsafe
                {
                    if ((eventPayload != null) && (eventPayload.Length != 0))
                    {
                        int argCount = eventPayload.Length;
                        if (argCount > etwMaxNumberArguments)
                        {
                            //
                            //too many arguments to log
                            //
                            throw Fx.Exception.AsError(new ArgumentOutOfRangeException("eventPayload",
                                                                                       InternalSR.EtwMaxNumberArgumentsExceeded(etwMaxNumberArguments)));
                        }

                        uint     totalEventSize = 0;
                        int      index;
                        int      stringIndex    = 0;
                        int[]    stringPosition = new int[etwAPIMaxStringCount];                                         //used to keep the position of strings in the eventPayload parameter
                        string[] dataString     = new string[etwAPIMaxStringCount];                                      // string arrays from the eventPayload parameter
                        UnsafeNativeMethods.EventData *userData    = stackalloc UnsafeNativeMethods.EventData[argCount]; // allocation for the data descriptors
                        UnsafeNativeMethods.EventData *userDataPtr = (UnsafeNativeMethods.EventData *)userData;
                        byte *dataBuffer    = stackalloc byte[basicTypeAllocationBufferSize * argCount];                 // 16 byte for unboxing non-string argument
                        byte *currentBuffer = dataBuffer;

                        //
                        // The loop below goes through all the arguments and fills in the data
                        // descriptors. For strings save the location in the dataString array.
                        // Caculates the total size of the event by adding the data descriptor
                        // size value set in EncodeObjec method.
                        //
                        for (index = 0; index < eventPayload.Length; index++)
                        {
                            if (eventPayload[index] != null)
                            {
                                string isString;
                                isString        = EncodeObject(ref eventPayload[index], userDataPtr, currentBuffer);
                                currentBuffer  += basicTypeAllocationBufferSize;
                                totalEventSize += userDataPtr->Size;
                                userDataPtr++;
                                if (isString != null)
                                {
                                    if (stringIndex < etwAPIMaxStringCount)
                                    {
                                        dataString[stringIndex]     = isString;
                                        stringPosition[stringIndex] = index;
                                        stringIndex++;
                                    }
                                    else
                                    {
                                        throw Fx.Exception.AsError(new ArgumentOutOfRangeException("eventPayload",
                                                                                                   InternalSR.EtwAPIMaxStringCountExceeded(etwAPIMaxStringCount)));
                                    }
                                }
                            }
                        }

                        if (totalEventSize > traceEventMaximumSize)
                        {
                            errorCode = WriteEventErrorCode.EventTooBig;
                            return(false);
                        }

                        fixed(char *v0 = dataString[0], v1 = dataString[1], v2 = dataString[2], v3 = dataString[3],
                              v4       = dataString[4], v5 = dataString[5], v6 = dataString[6], v7 = dataString[7])
                        {
                            userDataPtr = (UnsafeNativeMethods.EventData *)userData;
                            if (dataString[0] != null)
                            {
                                userDataPtr[stringPosition[0]].DataPointer = (ulong)v0;
                            }
                            if (dataString[1] != null)
                            {
                                userDataPtr[stringPosition[1]].DataPointer = (ulong)v1;
                            }
                            if (dataString[2] != null)
                            {
                                userDataPtr[stringPosition[2]].DataPointer = (ulong)v2;
                            }
                            if (dataString[3] != null)
                            {
                                userDataPtr[stringPosition[3]].DataPointer = (ulong)v3;
                            }
                            if (dataString[4] != null)
                            {
                                userDataPtr[stringPosition[4]].DataPointer = (ulong)v4;
                            }
                            if (dataString[5] != null)
                            {
                                userDataPtr[stringPosition[5]].DataPointer = (ulong)v5;
                            }
                            if (dataString[6] != null)
                            {
                                userDataPtr[stringPosition[6]].DataPointer = (ulong)v6;
                            }
                            if (dataString[7] != null)
                            {
                                userDataPtr[stringPosition[7]].DataPointer = (ulong)v7;
                            }

                            status = UnsafeNativeMethods.EventWriteTransfer(this.traceRegistrationHandle, ref eventDescriptor, ref eventTraceActivity.ActivityId, ref relatedActivityId, (uint)argCount, userData);
                        }
                    }
                    else
                    {
                        status = UnsafeNativeMethods.EventWriteTransfer(this.traceRegistrationHandle, ref eventDescriptor, ref eventTraceActivity.ActivityId, ref relatedActivityId, 0, null);
                    }
                }
            }

            if (status != 0)
            {
                SetLastError((int)status);
                return(false);
            }

            return(true);
        }
        public unsafe bool WriteTransferEvent(ref EventDescriptor eventDescriptor, EventTraceActivity eventTraceActivity, Guid relatedActivityId, object[] eventPayload)
        {
            if (eventTraceActivity == null)
            {
                eventTraceActivity = EventTraceActivity.Empty;
            }
            uint num = 0;

            if (this.IsEnabled(eventDescriptor.Level, eventDescriptor.Keywords))
            {
                if (eventPayload == null || (int)eventPayload.Length == 0)
                {
                    num = UnsafeNativeMethods.EventWriteTransfer(this.traceRegistrationHandle, ref eventDescriptor, ref eventTraceActivity.ActivityId, ref relatedActivityId, 0, 0);
                }
                else
                {
                    int length = (int)eventPayload.Length;
                    if (length <= 32)
                    {
                        uint     size      = 0;
                        int      num1      = 0;
                        int[]    numArray  = new int[8];
                        string[] strArrays = new string[8];
                        UnsafeNativeMethods.EventData *eventDataPointer  = (UnsafeNativeMethods.EventData *)length * sizeof(UnsafeNativeMethods.EventData);
                        UnsafeNativeMethods.EventData *eventDataPointer1 = eventDataPointer;
                        byte *numPointer  = (byte *)16 * length;
                        byte *numPointer1 = numPointer;
                        for (int i = 0; i < (int)eventPayload.Length; i++)
                        {
                            if (eventPayload[i] != null)
                            {
                                string str = DiagnosticsEventProvider.EncodeObject(ref eventPayload[i], eventDataPointer1, numPointer1);
                                numPointer1       = numPointer1 + 16;
                                size              = size + (*(eventDataPointer1)).Size;
                                eventDataPointer1 = (UnsafeNativeMethods.EventData *)(eventDataPointer1 + sizeof(UnsafeNativeMethods.EventData));
                                if (str != null)
                                {
                                    if (num1 >= 8)
                                    {
                                        throw Fx.Exception.AsError(new ArgumentOutOfRangeException("eventPayload", InternalSR.EtwAPIMaxStringCountExceeded(8)));
                                    }
                                    else
                                    {
                                        strArrays[num1] = str;
                                        numArray[num1]  = i;
                                        num1++;
                                    }
                                }
                            }
                        }
                        if (size <= 0xffca)
                        {
                            fixed(string str1 = strArrays[0])
                            {
                                string *strPointers        = &str1;
                                char *  offsetToStringData = (char *)(&str1);

                                if (&str1 != null)
                                {
                                    offsetToStringData = (char *)(strPointers + RuntimeHelpers.OffsetToStringData);
                                }
                                char *chrPointer = offsetToStringData;

                                fixed(string str2 = strArrays[1])
                                {
                                    string *strPointers1        = &str2;
                                    char *  offsetToStringData1 = (char *)(&str2);

                                    if (&str2 != null)
                                    {
                                        offsetToStringData1 = (char *)(strPointers1 + RuntimeHelpers.OffsetToStringData);
                                    }
                                    char *chrPointer1 = offsetToStringData1;

                                    fixed(string str3 = strArrays[2])
                                    {
                                        string *strPointers2        = &str3;
                                        char *  offsetToStringData2 = (char *)(&str3);

                                        if (&str3 != null)
                                        {
                                            offsetToStringData2 = (char *)(strPointers2 + RuntimeHelpers.OffsetToStringData);
                                        }
                                        char *chrPointer2 = offsetToStringData2;

                                        fixed(string str4 = strArrays[3])
                                        {
                                            string *strPointers3        = &str4;
                                            char *  offsetToStringData3 = (char *)(&str4);

                                            if (&str4 != null)
                                            {
                                                offsetToStringData3 = (char *)(strPointers3 + RuntimeHelpers.OffsetToStringData);
                                            }
                                            char *chrPointer3 = offsetToStringData3;

                                            fixed(string str5 = strArrays[4])
                                            {
                                                string *strPointers4        = &str5;
                                                char *  offsetToStringData4 = (char *)(&str5);

                                                if (&str5 != null)
                                                {
                                                    offsetToStringData4 = (char *)(strPointers4 + RuntimeHelpers.OffsetToStringData);
                                                }
                                                char *chrPointer4 = offsetToStringData4;

                                                fixed(string str6 = strArrays[5])
                                                {
                                                    string *strPointers5        = &str6;
                                                    char *  offsetToStringData5 = (char *)(&str6);

                                                    if (&str6 != null)
                                                    {
                                                        offsetToStringData5 = (char *)(strPointers5 + RuntimeHelpers.OffsetToStringData);
                                                    }
                                                    char *chrPointer5 = offsetToStringData5;

                                                    fixed(string str7 = strArrays[6])
                                                    {
                                                        string *strPointers6        = &str7;
                                                        char *  offsetToStringData6 = (char *)(&str7);

                                                        if (&str7 != null)
                                                        {
                                                            offsetToStringData6 = (char *)(strPointers6 + RuntimeHelpers.OffsetToStringData);
                                                        }
                                                        char *chrPointer6 = offsetToStringData6;

                                                        fixed(string str8 = strArrays[7])
                                                        {
                                                            string *strPointers7        = &str8;
                                                            char *  offsetToStringData7 = (char *)(&str8);

                                                            if (&str8 != null)
                                                            {
                                                                offsetToStringData7 = (char *)(strPointers7 + RuntimeHelpers.OffsetToStringData);
                                                            }
                                                            char *chrPointer7 = offsetToStringData7;

                                                            eventDataPointer1 = eventDataPointer;
                                                            if (strArrays[0] != null)
                                                            {
                                                                (eventDataPointer1 + numArray[0] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer;
                                                            }
                                                            if (strArrays[1] != null)
                                                            {
                                                                (eventDataPointer1 + numArray[1] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer1;
                                                            }
                                                            if (strArrays[2] != null)
                                                            {
                                                                (eventDataPointer1 + numArray[2] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer2;
                                                            }
                                                            if (strArrays[3] != null)
                                                            {
                                                                (eventDataPointer1 + numArray[3] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer3;
                                                            }
                                                            if (strArrays[4] != null)
                                                            {
                                                                (eventDataPointer1 + numArray[4] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer4;
                                                            }
                                                            if (strArrays[5] != null)
                                                            {
                                                                (eventDataPointer1 + numArray[5] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer5;
                                                            }
                                                            if (strArrays[6] != null)
                                                            {
                                                                (eventDataPointer1 + numArray[6] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer6;
                                                            }
                                                            if (strArrays[7] != null)
                                                            {
                                                                (eventDataPointer1 + numArray[7] * sizeof(UnsafeNativeMethods.EventData)).DataPointer = (ulong)chrPointer7;
                                                            }
                                                            num = UnsafeNativeMethods.EventWriteTransfer(this.traceRegistrationHandle, ref eventDescriptor, ref eventTraceActivity.ActivityId, ref relatedActivityId, length, eventDataPointer);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            DiagnosticsEventProvider.errorCode = DiagnosticsEventProvider.WriteEventErrorCode.EventTooBig;
                            return(false);
                        }
                    }
                    else
                    {
                        throw Fx.Exception.AsError(new ArgumentOutOfRangeException("eventPayload", InternalSR.EtwMaxNumberArgumentsExceeded(32)));
                    }
                }
            }
            if (num == 0)
            {
                return(true);
            }
            else
            {
                DiagnosticsEventProvider.SetLastError(num);
                return(false);
            }
        }
        public bool WriteEvent(ref EventDescriptor eventDescriptor, EventTraceActivity eventTraceActivity, params object[] eventPayload)
        {
            uint status = 0;

            if (IsEnabled(eventDescriptor.Level, eventDescriptor.Keywords))
            {
                int argCount = 0;

                if (eventTraceActivity != null)
                {
                    SetActivityId(ref eventTraceActivity.ActivityId);
                }

                unsafe
                {
                    if ((eventPayload == null) ||
                        (eventPayload.Length == 0) ||
                        (eventPayload.Length == 1))
                    {
                        string dataString = null;
                        UnsafeNativeMethods.EventData userData;

                        byte *dataBuffer = stackalloc byte[basicTypeAllocationBufferSize]; // Assume a max of 16 chars for non-string argument

                        userData.Size = 0;
                        if ((eventPayload != null) && (eventPayload.Length != 0))
                        {
                            //
                            // Figure out the type and fill the data descriptor
                            //
                            dataString = EncodeObject(ref eventPayload[0], &userData, dataBuffer);
                            argCount   = 1;
                        }

                        if (userData.Size > traceEventMaximumSize)
                        {
                            //
                            // Maximum size of the event payload plus header is 64k
                            //
                            errorCode = WriteEventErrorCode.EventTooBig;
                            return(false);
                        }

                        if (dataString != null)
                        {
                            fixed(char *pdata = dataString)
                            {
                                userData.DataPointer = (ulong)pdata;
                                status = UnsafeNativeMethods.EventWrite(this.traceRegistrationHandle, ref eventDescriptor, (uint)argCount, &userData);
                            }
                        }
                        else
                        {
                            if (argCount == 0)
                            {
                                status = UnsafeNativeMethods.EventWrite(this.traceRegistrationHandle, ref eventDescriptor, 0, null);
                            }
                            else
                            {
                                status = UnsafeNativeMethods.EventWrite(this.traceRegistrationHandle, ref eventDescriptor, (uint)argCount, &userData);
                            }
                        }
                    }
                    else
                    {
                        argCount = eventPayload.Length;

                        if (argCount > etwMaxNumberArguments)
                        {
                            //
                            //too many arguments to log
                            //
                            throw Fx.Exception.AsError(new ArgumentOutOfRangeException("eventPayload",
                                                                                       InternalSR.EtwMaxNumberArgumentsExceeded(etwMaxNumberArguments)));
                        }

                        uint     totalEventSize = 0;
                        int      index;
                        int      stringIndex    = 0;
                        int[]    stringPosition = new int[etwAPIMaxStringCount];
                        string[] dataString     = new string[etwAPIMaxStringCount];
                        UnsafeNativeMethods.EventData *userData    = stackalloc UnsafeNativeMethods.EventData[argCount];
                        UnsafeNativeMethods.EventData *userDataPtr = (UnsafeNativeMethods.EventData *)userData;
                        byte *dataBuffer    = stackalloc byte[basicTypeAllocationBufferSize * argCount]; // Assume 16 chars for non-string argument
                        byte *currentBuffer = dataBuffer;

                        //
                        // The loop below goes through all the arguments and fills in the data
                        // descriptors. For strings save the location in the dataString array.
                        // Caculates the total size of the event by adding the data descriptor
                        // size value set in EncodeObjec method.
                        //
                        for (index = 0; index < eventPayload.Length; index++)
                        {
                            if (eventPayload[index] != null)
                            {
                                string isString;
                                isString        = EncodeObject(ref eventPayload[index], userDataPtr, currentBuffer);
                                currentBuffer  += basicTypeAllocationBufferSize;
                                totalEventSize += userDataPtr->Size;
                                userDataPtr++;
                                if (isString != null)
                                {
                                    if (stringIndex < etwAPIMaxStringCount)
                                    {
                                        dataString[stringIndex]     = isString;
                                        stringPosition[stringIndex] = index;
                                        stringIndex++;
                                    }
                                    else
                                    {
                                        throw Fx.Exception.AsError(new ArgumentOutOfRangeException("eventPayload",
                                                                                                   InternalSR.EtwAPIMaxStringCountExceeded(etwAPIMaxStringCount)));
                                    }
                                }
                            }
                        }

                        if (totalEventSize > traceEventMaximumSize)
                        {
                            errorCode = WriteEventErrorCode.EventTooBig;
                            return(false);
                        }

                        //
                        // now fix any string arguments and set the pointer on the data descriptor
                        //
                        fixed(char *v0 = dataString[0], v1 = dataString[1], v2 = dataString[2], v3 = dataString[3],
                              v4       = dataString[4], v5 = dataString[5], v6 = dataString[6], v7 = dataString[7])
                        {
                            userDataPtr = (UnsafeNativeMethods.EventData *)userData;
                            if (dataString[0] != null)
                            {
                                userDataPtr[stringPosition[0]].DataPointer = (ulong)v0;
                            }
                            if (dataString[1] != null)
                            {
                                userDataPtr[stringPosition[1]].DataPointer = (ulong)v1;
                            }
                            if (dataString[2] != null)
                            {
                                userDataPtr[stringPosition[2]].DataPointer = (ulong)v2;
                            }
                            if (dataString[3] != null)
                            {
                                userDataPtr[stringPosition[3]].DataPointer = (ulong)v3;
                            }
                            if (dataString[4] != null)
                            {
                                userDataPtr[stringPosition[4]].DataPointer = (ulong)v4;
                            }
                            if (dataString[5] != null)
                            {
                                userDataPtr[stringPosition[5]].DataPointer = (ulong)v5;
                            }
                            if (dataString[6] != null)
                            {
                                userDataPtr[stringPosition[6]].DataPointer = (ulong)v6;
                            }
                            if (dataString[7] != null)
                            {
                                userDataPtr[stringPosition[7]].DataPointer = (ulong)v7;
                            }

                            status = UnsafeNativeMethods.EventWrite(this.traceRegistrationHandle, ref eventDescriptor, (uint)argCount, userData);
                        }
                    }
                }
            }

            if (status != 0)
            {
                SetLastError((int)status);
                return(false);
            }

            return(true);
        }