private static void RunReader(string filename, int maxLogsPerSec) { Console.WriteLine($"Monitoring {filename}"); using (var p = new LogReader(filename)) { var aggregator = new LogAggregator(p, maxLogsPerSec); aggregator.AggregateProcessed += (sender, agg) => WriteStatus(agg); aggregator.AlertTriggered += (sender, agg) => WriteAlertTriggered(agg); aggregator.AlertCancelled += (sender, agg) => WriteAlertCancelled(agg); var cts = new System.Threading.CancellationTokenSource(); var t = p.ReadFile(cts.Token); t.Wait(cts.Token); } }
private static void RunDemo(string filename, int delay, int maxLogsPerSec) { Console.WriteLine($"Demoing with {filename}"); using (var g = new LogWriter(filename, delay)) using (var p = new LogReader(filename)) { var aggregator = new LogAggregator(p, maxLogsPerSec); aggregator.AggregateProcessed += (sender, agg) => WriteStatus(agg); aggregator.AlertTriggered += (sender, agg) => WriteAlertTriggered(agg); aggregator.AlertCancelled += (sender, agg) => WriteAlertCancelled(agg); var cts = new System.Threading.CancellationTokenSource(); var tr = p.ReadFile(cts.Token); var tw = g.WriteFile(cts.Token); Task.WaitAll(tw, tr); } }
/// <summary> /// Constructs a new LogAggregator with the provided LogReader and configuration options. /// </summary> /// <param name="reader">A prepared LogReader instance that will provide the necessary log events</param> /// <param name="maxLogsPerSec">The maximum average LPS beyond which alerts should be triggered</param> /// <param name="postingDelay">Duration in milliseconds between emitting aggregates, aka how big the time box should be</param> /// <param name="rollingWindow">Duration in milliseconds for the rolling window used for averaging LPS, must be longer than the posting delay</param> public LogAggregator(LogReader reader, int maxLogsPerSec, int postingDelay = POSTING_DELAY, int rollingWindow = ROLLING_WINDOW) { this.MaxLogsPerSec = maxLogsPerSec; this.PostingDelay = postingDelay; this.RollingWindow = rollingWindow; this.Reader = reader; this.Current = new LogAggregate(); this.Aggregates = new List <LogAggregate>(); this.FlushTimer = new Timer(this.PostingDelay); // There's probably a cleaner way to use Task.Delay or the TPL Dataflow library to do this // But the timer event method works well and is conceptually simple this.FlushTimer.Elapsed += async(sender, e) => await Task.Run(() => ProcessAggregate()); this.Reader.LogParsed += (sender, line) => ProcessLog(line); this.FlushTimer.Start(); }