/// <summary>
        /// Do intialization common to the contructors.
        /// </summary>

        private void Init(string sessionName)
        {
            this.sessionHandle = TraceEventNativeMethods.INVALID_HANDLE_VALUE;
            if (sessionName.Length > MaxNameSize - 1)
            {
                throw new ArgumentException("File name too long", "sessionName");
            }

            this.sessionName             = sessionName;
            properties                   = new TraceEventNativeMethods.EVENT_TRACE_PROPERTIES();
            properties.Wnode.Flags       = TraceEventNativeMethods.WNODE_FLAG_TRACED_GUID;
            properties.LoggerNameOffset  = (uint)Marshal.SizeOf(typeof(TraceEventNativeMethods.EVENT_TRACE_PROPERTIES));
            properties.LogFileNameOffset = properties.LoggerNameOffset + MaxNameSize * sizeof(char);
            extentionSpaceOffset         = properties.LogFileNameOffset + MaxNameSize * sizeof(char);
            // #sizeCalculation
            properties.Wnode.BufferSize = extentionSpaceOffset + 8 + 8 * 4 + 4 + maxStackTraceProviders * 4;
            unmanagedPropertiesBuffer   = TraceEventNativeMethods.AllocHGlobal((int)properties.Wnode.BufferSize);
            TraceEventNativeMethods.ZeroMemory(unmanagedPropertiesBuffer, properties.Wnode.BufferSize);
        }
        private IntPtr ToUnmanagedBuffer(TraceEventNativeMethods.EVENT_TRACE_PROPERTIES properties, string fileName)
        {
            if (fileName == null)
            {
                properties.LogFileNameOffset = 0;
            }
            else
            {
                properties.LogFileNameOffset = properties.LoggerNameOffset + MaxNameSize * 2;
            }

            Marshal.StructureToPtr(properties, unmanagedPropertiesBuffer, false);
            byte[] buffer = Encoding.Unicode.GetBytes(sessionName);
            Marshal.Copy(buffer, 0, (IntPtr)(unmanagedPropertiesBuffer.ToInt64() + properties.LoggerNameOffset), buffer.Length);
            Marshal.WriteInt16(unmanagedPropertiesBuffer, (int)properties.LoggerNameOffset + buffer.Length, 0);

            if (fileName != null)
            {
                buffer = Encoding.Unicode.GetBytes(fileName);
                Marshal.Copy(buffer, 0, (IntPtr)(unmanagedPropertiesBuffer.ToInt64() + properties.LogFileNameOffset), buffer.Length);
                Marshal.WriteInt16(unmanagedPropertiesBuffer, (int)properties.LogFileNameOffset + buffer.Length, 0);
            }
            return(unmanagedPropertiesBuffer);
        }