private void ProcessTraceInBackground(TraceSafeHandle traceInternalHandle) { Exception asyncException = null; ulong[] array = { traceInternalHandle.UnsafeValue }; try { // Begin receiving the events handled by EventRecordCallback. // It is a blocking call until the trace handle gets closed. var status = NativeMethods.ProcessTrace(array, 1, IntPtr.Zero, IntPtr.Zero); if (status != 0) { const int ErrorInvalidHandle = 6; if (status == ErrorInvalidHandle) { // The handle was closed before starting processing. } else { // Throw the exception to capture the stack. throw new Win32Exception(status); } } } catch (Exception exception) { asyncException = exception; } // Send exception to subscribers. var eventArrived = this.EventArrived; if (asyncException != null && eventArrived != null) { try { eventArrived(this, new EventArrivedEventArgs(asyncException)); } catch (Exception exception) { if (exception is ThreadAbortException || exception is OutOfMemoryException || exception is StackOverflowException) { throw; } // Never fail because non-critical exceptions thown by this method // can be rethrow during disposing of this object. Debug.Assert(false, "Exception was thrown from ProcessEventArrived handler", exception.ToString()); } } }
private void StopTracing() { if (this.traceHandle != null) { this.traceHandle.Dispose(); this.traceHandle = null; } if (this.sessionHandle != null) { this.sessionHandle.Dispose(); this.sessionHandle = null; } // Once the unmanaged resources got released, end the process trace thread // that may throw exception (e.g. OOM). if (this.processEventsDelgate != null && this.asyncResult != null) { this.processEventsDelgate.EndInvoke(this.asyncResult); } }
private void StartTracing() { const uint RealTime = 0x00000100; const uint EventRecord = 0x10000000; const uint BufferSize = 64; const uint MinBuffers = 20; const uint MaxBuffers = 200; const uint FlushTimerSeconds = 1; int status; if (!LoadExistingEventTraceProperties()) { this.eventTraceProperties.SetParameters(RealTime, BufferSize, MinBuffers, MaxBuffers, FlushTimerSeconds); // Start trace session ulong unsafeSessionHandle; status = NativeMethods.StartTrace(out unsafeSessionHandle, this.loggerName, ref this.eventTraceProperties); if (status != 0) { throw new System.ComponentModel.Win32Exception(status); } this.sessionHandle = new SessionSafeHandle(unsafeSessionHandle, this.loggerName); Guid EmptyGuid = Guid.Empty; Version Windows7Version = new Version(6, 1, 7600); if (Environment.OSVersion.Version.CompareTo(Windows7Version) >= 0) { const int TimeToWaitForInitialize = 10 * 1000; EnableTraceParameters enableParameters = new EnableTraceParameters(); enableParameters.Version = 1; // ENABLE_TRACE_PARAMETERS_VERSION enableParameters.EnableProperty = EventEnableProperty.Sid; status = NativeMethods.EnableTraceEx2( unsafeSessionHandle, ref this.eventProviderId, 1, // controlCode - EVENT_CONTROL_CODE_ENABLE_PROVIDER (byte)this.Level, this.MatchAnyKeyword, 0, // matchAnyKeyword TimeToWaitForInitialize, ref enableParameters); } else { status = NativeMethods.EnableTraceEx( ref this.eventProviderId, ref EmptyGuid, // sourceId unsafeSessionHandle, 1, // isEnabled (byte)this.Level, this.MatchAnyKeyword, 0, // matchAllKeywords EventEnableProperty.Sid, IntPtr.Zero); } if (status != 0) { throw new System.ComponentModel.Win32Exception(status); } } this.logFile = new EventTraceLogfile(); this.logFile.LoggerName = this.loggerName; this.logFile.EventRecordCallback = EventRecordCallback; this.logFile.ProcessTraceMode = EventRecord | RealTime; ulong unsafeTraceHandle = NativeMethods.OpenTrace(ref this.logFile); status = Marshal.GetLastWin32Error(); if (status != 0) { throw new System.ComponentModel.Win32Exception(status); } this.traceHandle = new TraceSafeHandle(unsafeTraceHandle); this.processEventsDelgate = new ProcessTraceDelegate(ProcessTraceInBackground); this.asyncResult = this.processEventsDelgate.BeginInvoke(this.traceHandle, null, this.processEventsDelgate); }
private void ProcessTraceInBackground(TraceSafeHandle traceHandle) { Exception asyncException = null; ulong[] array = { traceHandle.UnsafeValue }; try { // Begin receiving the events handled by EventRecordCallback. // It is a blocking call until the trace handle gets closed. int status = NativeMethods.ProcessTrace(array, 1, IntPtr.Zero, IntPtr.Zero); if (status != 0) { const int ERROR_INVALID_HANDLE = 6; if (status == ERROR_INVALID_HANDLE) { // The handle was closed before starting processing. } else { // Throw the exception to capture the stack. throw new Win32Exception(status); } } } catch (Exception exception) { asyncException = exception; } // Send exception to subscribers. EventHandler<EventArrivedEventArgs> eventArrived = this.EventArrived; if (asyncException != null && eventArrived != null) { try { eventArrived(this, new EventArrivedEventArgs(asyncException)); } catch (Exception exception) { if (exception is ThreadAbortException || exception is OutOfMemoryException || exception is StackOverflowException) { throw; } // Never fail because non-critical exceptions thown by this method // can be rethrow during disposing of this object. Debug.Assert(false, "Exception was thrown from ProcessEventArrived handler", exception.ToString()); } } }
private void StartTracing() { const uint RealTime = 0x00000100; const uint EventRecord = 0x10000000; const uint BufferSize = 64; const uint MinBuffers = 20; const uint MaxBuffers = 200; const uint FlushTimerSeconds = 1; int status; if (!LoadExistingEventTraceProperties()) { _eventTraceProperties.SetParameters(RealTime, BufferSize, MinBuffers, MaxBuffers, FlushTimerSeconds); // Start trace session ulong unsafeSessionHandle; status = NativeMethods.StartTrace( out unsafeSessionHandle, _loggerName, ref _eventTraceProperties); if (status != 0) { throw new Win32Exception(status); } _sessionHandle = new SessionSafeHandle(unsafeSessionHandle, _loggerName); var emptyGuid = Guid.Empty; var windows7Version = new Version(6, 1, 7600); if (Environment.OSVersion.Version.CompareTo(windows7Version) >= 0) { const int TimeToWaitForInitialize = 10 * 1000; var enableParameters = new EnableTraceParameters { Version = 1, EnableProperty = EventEnableProperty.Sid }; // ENABLE_TRACE_PARAMETERS_VERSION status = NativeMethods.EnableTraceEx2( unsafeSessionHandle, ref _eventProviderId, 1, // controlCode - EVENT_CONTROL_CODE_ENABLE_PROVIDER (byte)Level, MatchAnyKeyword, 0, // matchAnyKeyword TimeToWaitForInitialize, ref enableParameters); } else { status = NativeMethods.EnableTraceEx( ref _eventProviderId, ref emptyGuid, // sourceId unsafeSessionHandle, 1, // isEnabled (byte)Level, MatchAnyKeyword, 0, // matchAllKeywords EventEnableProperty.Sid, IntPtr.Zero); } if (status != 0) { throw new Win32Exception(status); } } _logFile = new EventTraceLogfile { LoggerName = _loggerName, EventRecordCallback = EventRecordCallback, ProcessTraceMode = EventRecord | RealTime }; var unsafeTraceHandle = NativeMethods.OpenTrace(ref _logFile); status = Marshal.GetLastWin32Error(); if (status != 0) { throw new Win32Exception(status); } _traceHandle = new TraceSafeHandle(unsafeTraceHandle); _processEventsDelgate = ProcessTraceInBackground; _asyncResult = _processEventsDelgate.BeginInvoke(_traceHandle, null, _processEventsDelgate); }