예제 #1
0
        /// <summary>
        /// Register a performance counter for collection.
        /// </summary>
        /// <param name="originalString">Original string definition of the counter.</param>
        /// <param name="reportAs">Alias to report the counter as.</param>
        /// <param name="categoryName">Category name.</param>
        /// <param name="counterName">Counter name.</param>
        /// <param name="instanceName">Instance name.</param>
        /// <param name="usesInstanceNamePlaceholder">Indicates whether the counter uses a placeholder in the instance name.</param>
        private void RegisterPerformanceCounter(string originalString, string reportAs, string categoryName, string counterName, string instanceName, bool usesInstanceNamePlaceholder)
        {
            ICounterValue performanceCounter = null;

            try
            {
                performanceCounter = CounterFactory.GetCounter(originalString, categoryName, counterName, instanceName);
            }
            catch (Exception e)
            {
                // we want to have another crack at it if instance placeholder is used,
                // notably due to the fact that CLR process ID counter only starts returning values after the first garbage collection
                if (!usesInstanceNamePlaceholder)
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  "Failed to register performance counter. Category: {0}, counter: {1}, instance: {2}.",
                                  categoryName,
                                  counterName,
                                  instanceName),
                              e);
                }
            }

            bool firstReadOk = false;

            try
            {
                // perform the first read. For many counters the first read will always return 0
                // since a single sample is not enough to calculate a value
                performanceCounter.Collect();

                firstReadOk = true;
            }
            catch (Exception e)
            {
                throw new InvalidOperationException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              "Failed to perform the first read for performance counter. Please make sure it exists. Category: {0}, counter: {1}, instance {2}",
                              categoryName,
                              counterName,
                              instanceName),
                          e);
            }
            finally
            {
                PerformanceCounterData perfData = new PerformanceCounterData(
                    originalString,
                    reportAs,
                    usesInstanceNamePlaceholder,
                    !firstReadOk,
                    categoryName,
                    counterName,
                    instanceName);

                this.performanceCounters.Add(new Tuple <PerformanceCounterData, ICounterValue>(perfData, performanceCounter));
            }
        }
 /// <summary>
 /// Collects a value for a single counter.
 /// </summary>
 private static double CollectCounter(string coutnerOriginalString, ICounterValue counter)
 {
     try
     {
         return(counter.Collect());
     }
     catch (Exception e)
     {
         throw new InvalidOperationException(
                   string.Format(
                       CultureInfo.CurrentCulture,
                       "Failed to perform a read for web app performance counter {0}",
                       coutnerOriginalString),
                   e);
     }
 }
        /// <summary>
        /// Register a performance counter for collection.
        /// </summary>
        private void RegisterPerformanceCounter(string originalString, string reportAs, string categoryName, string counterName, string instanceName, bool usesInstanceNamePlaceholder)
        {
            ICounterValue counter = null;

            try
            {
                counter = CounterFactory.GetCounter(originalString, reportAs);
            }
            catch
            {
                PerformanceCollectorEventSource.Log.CounterNotWebAppSupported(originalString);
                return;
            }

            bool firstReadOk = false;

            try
            {
                // perform the first read. For many counters the first read will always return 0
                // since a single sample is not enough to calculate a value
                var value = counter.Collect();
                firstReadOk = true;
            }
            catch (Exception e)
            {
                throw new InvalidOperationException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              "Failed to perform the first read for web app performance counter. Please make sure it exists. Counter: {0}",
                              counterName),
                          e);
            }
            finally
            {
                PerformanceCounterData perfData = new PerformanceCounterData(
                    originalString,
                    reportAs,
                    usesInstanceNamePlaceholder,
                    !firstReadOk,
                    categoryName,
                    counterName,
                    instanceName);

                this.performanceCounters.Add(new Tuple <PerformanceCounterData, ICounterValue>(perfData, counter));
            }
        }