using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; namespace Sony.Filtr.Tasks.Tasks { public class ActionTracker { public readonly string FilePath; private readonly BatchBlock actionTrackerBatcher; private readonly ActionBlock> actionTracker; public ActionTracker(string filePath, int logsToTrace) { this.FilePath = filePath; this.actionTrackerBatcher = new BatchBlock(logsToTrace); this.actionTracker = new ActionBlock>( logs => File.WriteAllLines(this.FilePath, logs.ToArray()), new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = 1 }); this.actionTrackerBatcher.LinkTo(this.actionTracker, new DataflowLinkOptions() { PropagateCompletion = true }); } public async Task CompleteAsync() { this.actionTrackerBatcher.Complete(); await this.actionTracker.Completion; } public void AddTrace(string message) { if (!string.IsNullOrWhiteSpace(message)) { this.actionTrackerBatcher.SendAsync(message); } } } }