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); } }
public void Dispose() { if (!EventTracingApi.IsInvalidProcessTraceHandle(TraceHandle)) { EventTracingApi.CloseTrace(TraceHandle); } }