private void Report(string counterName, bool success) { CounterReport counterReport = null; if (currentCounters.TryGetValue(counterName, out counterReport) == false) { lock (currentCounters) { if (currentCounters.TryGetValue(counterName, out counterReport) == false) { counterReport = new CounterReport(); currentCounters[counterName] = counterReport; } } } if (success) { System.Threading.Interlocked.Increment(ref counterReport.SuccessCount); } else { System.Threading.Interlocked.Increment(ref counterReport.FailureCount); } }
private void notifyTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { if (notify) { CounterNotification notification = new CounterNotification(); lock (currentCounters) { foreach (var kvp in currentCounters) { var report = new CounterReport() { Name = kvp.Key, SuccessCount = kvp.Value.SuccessCount, FailureCount = kvp.Value.FailureCount }; if (report.SuccessCount > 0 || report.FailureCount > 0) { Interlocked.Add(ref kvp.Value.SuccessCount, -report.SuccessCount); Interlocked.Add(ref kvp.Value.FailureCount, -report.FailureCount); notification.Counters.Add(report); } } } if (notification.Counters.Any()) { using (var channel = _bus.OpenPublishChannel()) { channel.Publish(notification); } } } } finally { notifyTimer.Start(); } }