/// <summary> /// Performs collection for all registered counters. /// </summary> /// <param name="onReadingFailure">Invoked when an individual counter fails to be read.</param> public IEnumerable <Tuple <PerformanceCounterData, float> > Collect( Action <string, Exception> onReadingFailure = null) { return(this.performanceCounters.Where(pc => !pc.IsInBadState).SelectMany( pc => { float value; try { value = CollectCounter(pc.PerformanceCounter); } catch (InvalidOperationException e) { if (onReadingFailure != null) { onReadingFailure( PerformanceCounterUtility.FormatPerformanceCounter(pc.PerformanceCounter), e); } return new Tuple <PerformanceCounterData, float>[] { }; } return new[] { Tuple.Create(pc, value) }; })); }
/// <summary> /// Collects a value for a single counter. /// </summary> private static float CollectCounter(PerformanceCounter pc) { try { return(pc.NextValue()); } catch (Exception e) { throw new InvalidOperationException( string.Format( CultureInfo.CurrentCulture, Resources.PerformanceCounterReadFailed, PerformanceCounterUtility.FormatPerformanceCounter(pc)), e); } }