Пример #1
0
        public EventRealtimeListener(Guid providerGuid, string sessionName, EtwTraceLevel level, long keyword)
        {
            if (providerGuid == Guid.Empty)
            {
                throw new ArgumentException(null, nameof(providerGuid));
            }

            if (sessionName == null)
            {
                throw new ArgumentNullException(nameof(sessionName));
            }

            _cb  = OnEvent;
            _rcb = OnRecordEvent;

            ProviderGuid = providerGuid;
            SessionName  = sessionName;

            int    status;
            IntPtr properties = BuildProperties(false, out int size);

            try
            {
                status = StartTrace(out _handle, SessionName, properties);
                if (status != 0)
                {
                    if (status != ERROR_ALREADY_EXISTS)
                    {
                        throw new Win32Exception(status);
                    }

                    // this can happen if something went wrong on another session with the same name
                    // so let's try to stop this existing thing and restart
                    StopTrace();

                    status = StartTrace(out _handle, SessionName, properties);
                    if (status != 0)
                    {
                        throw new Win32Exception(status);
                    }
                }
            }
            catch (Exception e)
            {
                App.AddTrace("EventRealtimeListener e:" + e);
                return;
            }
            finally
            {
                Marshal.FreeCoTaskMem(properties);
            }

            status = EnableTraceEx(providerGuid, IntPtr.Zero, _handle, 1, (byte)level, keyword, keyword, 0, IntPtr.Zero);
            if (status != 0)
            {
                throw new Win32Exception(status);
            }

            _traceOn = true;
        }
Пример #2
0
        private void OpenEtlFile()
        {
            // Retained the delegate objects to avoid GC.
            EventRecordCallbackRetainer = new EventRecordCallback(EventRecordCallback);
            BufferCallbackRetainer      = new BufferCallback(BufferCallback);

            // Open the ETL log file.
            EVENT_TRACE_LOGFILE traceLogFile = new EVENT_TRACE_LOGFILE
            {
                LogFileName      = EtlFilePath,
                ProcessTraceMode = EventTracingApi.PROCESS_TRACE_MODE_EVENT_RECORD,
                BufferCallback   = BufferCallbackRetainer,
                EventCallback    = EventRecordCallbackRetainer,
            };

            TraceHandle = EventTracingApi.OpenTrace(ref traceLogFile);

            if (EventTracingApi.IsInvalidProcessTraceHandle(TraceHandle))
            {
                var win32ErrorCode = Marshal.GetLastWin32Error();

                string exceptionMessage;
                if (win32ErrorCode == Win32ErrorCode.ERROR_FILE_CORRUPT)
                {
                    exceptionMessage = string.Format("OpenTrace function failed. The file '{0}' is corrupted.", EtlFilePath);
                }
                else
                {
                    exceptionMessage = string.Format("OpenTrace function failed. The file tried to open was '{0}'.", EtlFilePath);
                }

                throw new Win32Exception(win32ErrorCode, exceptionMessage);
            }
        }
Пример #3
0
        public void Open()
        {
            Guid localProviderId = this.providerInfo.Id;
            int  errorCode       = NativeMethods.EnableTraceEx(
                ref localProviderId,
                IntPtr.Zero,
                this.sessionHandle,
                1,
                this.providerInfo.Level,
                (ulong)this.providerInfo.Keywords,
                0,
                0,
                IntPtr.Zero);

            if (errorCode != 0)
            {
                throw new Win32Exception();
            }

            this.traceEnabled = true;
            EventTraceLogfile logFile = new EventTraceLogfile();

            logFile.ProcessTraceMode = TraceSession.ProcessTraceModeRealTime | TraceSession.ProcessTraceModeEventRecord;
            logFile.LoggerName       = this.sessionName;

            // The callback must be pinned to keep it in scope outside of this method
            EventRecordCallback callback = new EventRecordCallback(this.EventRecordCallback);

            this.callbackHandle         = GCHandle.Alloc(callback);
            logFile.EventRecordCallback = callback;

            this.traceHandle = NativeMethods.OpenTrace(ref logFile);
            if (this.traceHandle == InvalidHandle)
            {
                throw new Win32Exception();
            }
        }
Пример #4
0
        private void InitializeTraceHandle()
        {
            if (traceHandle == 0)
            {
                this.eventTraceLogFile = new EventTraceLogfile
                {
                    ProcessTraceMode = TraceModeEventRecord,
                    LogFileName      = this.fileName
                };

                // wrap callback in GCHandle to prevent object from being garbage collected
                EventRecordCallback callback = this.EventRecordCallback;
                this.callbackHandle = GCHandle.Alloc(callback);
                this.eventTraceLogFile.EventRecordCallback = this.EventRecordCallback;

                this.traceHandle = NativeMethods.OpenTrace(ref eventTraceLogFile);
                if (this.traceHandle == InvalidHandle)
                {
                    throw new Win32Exception();
                }

                this.EventsLost = this.eventTraceLogFile.LogfileHeader.EventsLost;
            }
        }