/// <summary> /// Async consumer processing of trace events in the blocking /// collection. /// </summary> private void CreateAndRunTraceEventConsumerTask() { Task.Run(() => { while (this.inMemorySink.Count > 0 || !this.inMemorySink.IsCompleted) { try { DecodedEventWrapper decodedEtwEventWrapper = this.inMemorySink.Take(); this.consumerProcessTraceEventAction(decodedEtwEventWrapper, this.traceFileSubFolder); } catch (InvalidOperationException) { // Ignore exception when Take is invoked on a queue that is marked as completed } catch (Exception ex) { this.traceSource.WriteError( this.logSourceId, "Failed to process trace event. {0}", ex); } } this.consumerProcessingCompleted.Set(); }); }
/// <summary> /// Action passed to EtlToInMemoryBufferWriter that /// writes the decoded event to a memory stream. /// </summary> /// <param name="decodedEventWrapper"></param> internal void ProcessTraceEvent(DecodedEventWrapper decodedEventWrapper) { if (this.stopping) { this.traceSource.WriteInfo( this.logSourceId, "The consumer is being stopped. Therefore, no more in-memory trace events will be processed in this pass."); return; } try { this.WriteEvent(decodedEventWrapper.StringRepresentation.Replace("\r\n", "\r\t").Replace("\n", "\t")); } catch (Exception e) { this.traceSource.WriteExceptionAsError( this.logSourceId, e, "Failed to write filtered event {0} to memory stream.", decodedEventWrapper.StringRepresentation); } // update last event index processed this.lastEventIndexProcessed.Set(decodedEventWrapper.Timestamp, decodedEventWrapper.TimestampDifferentiator); }
private void ProcessTraceEvent(DecodedEventWrapper decodedEventWrapper, string traceFileSubFolder) { if (this.stopping) { this.traceSource.WriteInfo( this.logSourceId, "The consumer is being stopped. Therefore, no more in-memory trace events will be processed in this pass."); return; } this.blobUploader.ProcessTraceEvent(decodedEventWrapper); }
internal void Add(DecodedEventWrapper decodedEtwEventWrapper) { this.inMemorySink.Add(decodedEtwEventWrapper); }
/// <summary> /// Action passed to EtlToInMemoryBufferWriter that /// writes the decoded event to the MDS table and updates the bookmark with the event index /// </summary> /// <param name="decodedEventWrapper"></param> /// <param name="traceFileSubFolder"></param> private void ProcessTraceEvent(DecodedEventWrapper decodedEventWrapper, string traceFileSubFolder) { if (this.stopping) { this.traceSource.WriteInfo( this.logSourceId, "The consumer is being stopped. Therefore, no more in-memory trace events will be processed in this pass."); return; } // retrieve bookmark folder string bookmarkFolder = this.progressManager.BookmarkFolders[traceFileSubFolder]; if (string.IsNullOrEmpty(bookmarkFolder)) { return; } // Get the MDS table writer MdsTableWriter mdsTableWriter; if (false == this.GetOrCreateMdsTableWriter(traceFileSubFolder, out mdsTableWriter)) { return; } Debug.Assert(null != mdsTableWriter, "MdsTableWriter should be set on success."); // Last event read position long bookmarkLastEventReadPosition = this.progressManager.BookmarkLastEventReadPosition[traceFileSubFolder]; string etwEvent = decodedEventWrapper.StringRepresentation.Replace("\r\n", "\r\t").Replace("\n", "\t"); // Parse the event string[] etwEventParts = etwEvent.Split(new[] { ',' }, (int)EtwEventParts.Count); if (etwEventParts.Length != (int)EtwEventParts.Count) { this.traceSource.WriteWarning( this.logSourceId, "The event is not in the expected format. Event info: {0}", etwEvent); return; } DateTime eventTime; if (false == DateTime.TryParse(etwEventParts[(int)EtwEventParts.Timestamp], out eventTime)) { this.traceSource.WriteWarning( this.logSourceId, "The event has invalid timestamp {0}", etwEventParts[(int)EtwEventParts.Timestamp]); return; } string eventLevel = etwEventParts[(int)EtwEventParts.Level]; int threadId; if (false == int.TryParse(etwEventParts[(int)EtwEventParts.ThreadId], out threadId)) { this.traceSource.WriteWarning( this.logSourceId, "The event has invalid thread ID {0}", etwEventParts[(int)EtwEventParts.ThreadId]); return; } int processId; if (false == int.TryParse(etwEventParts[(int)EtwEventParts.ProcessId], out processId)) { this.traceSource.WriteWarning( this.logSourceId, "The event has invalid process ID {0}", etwEventParts[(int)EtwEventParts.ProcessId]); return; } string eventText = etwEventParts[(int)EtwEventParts.Text]; string taskName; string eventType; string id; if (false == this.GetWinFabIdParts(etwEventParts[(int)EtwEventParts.WinFabId], out taskName, out eventType, out id)) { return; } // Initialize the fields for the current event in the MDS table long mdsEventBytesCount = 0; object[] tableFields = new object[(int)MdsTableFields.Count]; tableFields[(int)MdsTableFields.Timestamp] = eventTime.ToLocalTime(); // MDS expects local time mdsEventBytesCount += eventTime.ToBytes().Length; tableFields[(int)MdsTableFields.Level] = eventLevel; mdsEventBytesCount += eventLevel.Length; tableFields[(int)MdsTableFields.ProcessId] = processId; mdsEventBytesCount += sizeof(int); tableFields[(int)MdsTableFields.ThreadId] = threadId; mdsEventBytesCount += sizeof(int); tableFields[(int)MdsTableFields.TaskName] = taskName; mdsEventBytesCount += taskName.Length; tableFields[(int)MdsTableFields.EventType] = eventType; tableFields[(int)MdsTableFields.Id] = id; mdsEventBytesCount += id.Length; tableFields[(int)MdsTableFields.Text] = eventText; mdsEventBytesCount += eventText.Length; this.bookmarkBatchCount++; // Write the event to the MDS table var result = mdsTableWriter.WriteEvent(tableFields); if (MdsTableWriter.MdNoError != result) { this.mdsFileProducerPerf.MdsWriteError(); } else { this.mdsFileProducerPerf.BytesWritten(mdsEventBytesCount); this.mdsFileProducerPerf.MdsWriteSuccess(); } this.lastEventIndexProcessed.Set(decodedEventWrapper.Timestamp, decodedEventWrapper.TimestampDifferentiator); // Update the bookmark to indicate that we have processed the last event // written when we reach the bookmark batch size if (this.bookmarkBatchCount == this.mdsFileProducerSettings.BookmarkBatchSize) { this.bookmarkBatchCount = 0; long bytesWritten = 0; this.progressManager.UpdateBookmarkFile( bookmarkFolder, bookmarkLastEventReadPosition, this.lastEventIndexProcessed, out bytesWritten); this.mdsFileProducerPerf.BytesWritten(bytesWritten); } }