/// <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> /// Validates the counter by parsing. /// </summary> /// <param name="perfCounterName">Performance counter name to validate.</param> /// <param name="win32Instances">Windows 32 instances.</param> /// <param name="clrInstances">CLR instances.</param> /// <param name="usesInstanceNamePlaceholder">Boolean to check if it is using an instance name place holder.</param> /// <param name="error">Error message.</param> /// <returns>Performance counter.</returns> public static PerformanceCounter CreateAndValidateCounter( string perfCounterName, IEnumerable <string> win32Instances, IEnumerable <string> clrInstances, out bool usesInstanceNamePlaceholder, out string error) { error = null; try { return(PerformanceCounterUtility.ParsePerformanceCounter( perfCounterName, win32Instances, clrInstances, out usesInstanceNamePlaceholder)); } catch (Exception e) { usesInstanceNamePlaceholder = false; PerformanceCollectorEventSource.Log.CounterParsingFailedEvent(e.Message, perfCounterName); error = e.Message; return(null); } }
public static IPerformanceCollector GetPerformanceCollector() { IPerformanceCollector collector; if (PerformanceCounterUtility.IsWebAppRunningInAzure()) { if (PerformanceCounterUtility.IsWindows) { // WebApp For windows collector = (IPerformanceCollector) new WebAppPerformanceCollector(); PerformanceCollectorEventSource.Log.InitializedWithCollector(collector.GetType().Name); } else { // We are in WebApp, but not Windows. Use XPlatformPerfCollector. collector = (IPerformanceCollector) new PerformanceCollectorXPlatform(); PerformanceCollectorEventSource.Log.InitializedWithCollector(collector.GetType().Name); } } else if (PerformanceCounterUtility.IsWindows) { // The original Windows PerformanceCounter collector which is also // supported in NetStandard2.0 in Windows. collector = (IPerformanceCollector) new StandardPerformanceCollector(); PerformanceCollectorEventSource.Log.InitializedWithCollector(collector.GetType().Name); } else { // This is NetStandard2.0 and non-windows. Use XPlatformPerfCollector collector = (IPerformanceCollector) new PerformanceCollectorXPlatform(); PerformanceCollectorEventSource.Log.InitializedWithCollector(collector.GetType().Name); } return(collector); }
/// <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); } }
public static IPerformanceCollector GetPerformanceCollector() { IPerformanceCollector collector; if (PerformanceCounterUtility.IsWebAppRunningInAzure()) { collector = (IPerformanceCollector) new WebAppPerfCollector.WebAppPerformanceCollector(); PerformanceCollectorEventSource.Log.InitializedWithCollector(collector.GetType().Name); } else { collector = (IPerformanceCollector) new StandardPerformanceCollector(); PerformanceCollectorEventSource.Log.InitializedWithCollector(collector.GetType().Name); } return(collector); }
public static IPerformanceCollector GetPerformanceCollector() { IPerformanceCollector collector; // NetStandard1.6 has perf counter only on web apps. if (PerformanceCounterUtility.IsWebAppRunningInAzure()) { collector = (IPerformanceCollector) new WebAppPerformanceCollector(); PerformanceCollectorEventSource.Log.InitializedWithCollector(collector.GetType().Name); } else { // This will be the Stub collector which won't do anything. collector = (IPerformanceCollector) new StandardPerformanceCollectorStub(); PerformanceCollectorEventSource.Log.InitializedWithCollector(collector.GetType().Name); } return(collector); }