Beispiel #1
0
        /// <summary>
        ///     Returns an array of counters in this category for the given instance.
        /// </summary>
        public PerformanceCounter[] GetCounters(string instanceName)
        {
            if (instanceName == null)
            {
                throw new ArgumentNullException(nameof(instanceName));
            }

            if (_categoryName == null)
            {
                throw new InvalidOperationException(SR.CategoryNameNotSet);
            }

            if (instanceName.Length != 0 && !InstanceExists(instanceName))
            {
                throw new InvalidOperationException(SR.Format(SR.MissingInstance, instanceName, _categoryName));
            }

            string[]             counterNames = PerformanceCounterLib.GetCounters(_machineName, _categoryName);
            PerformanceCounter[] counters     = new PerformanceCounter[counterNames.Length];
            for (int index = 0; index < counters.Length; index++)
            {
                counters[index] = new PerformanceCounter(_categoryName, counterNames[index], instanceName, _machineName, true);
            }

            return(counters);
        }
 public PerformanceCounter[] GetCounters(string instanceName)
 {
     if (instanceName == null)
     {
         throw new ArgumentNullException("instanceName");
     }
     if (this.categoryName == null)
     {
         throw new InvalidOperationException(SR.GetString("CategoryNameNotSet"));
     }
     if ((instanceName.Length != 0) && !this.InstanceExists(instanceName))
     {
         throw new InvalidOperationException(SR.GetString("MissingInstance", new object[] { instanceName, this.categoryName }));
     }
     string[]             counters     = PerformanceCounterLib.GetCounters(this.machineName, this.categoryName);
     PerformanceCounter[] counterArray = new PerformanceCounter[counters.Length];
     for (int i = 0; i < counterArray.Length; i++)
     {
         counterArray[i] = new PerformanceCounter(this.categoryName, counters[i], instanceName, this.machineName, true);
     }
     return(counterArray);
 }
        internal static string[] GetCounters(string machine, string category)
        {
            PerformanceCounterLib performanceCounterLib = GetPerformanceCounterLib(machine, new CultureInfo(9));
            bool categoryExists = false;

            string[] counters = performanceCounterLib.GetCounters(category, ref categoryExists);
            if (!categoryExists && (CultureInfo.CurrentCulture.Parent.LCID != 9))
            {
                for (CultureInfo info = CultureInfo.CurrentCulture; info != CultureInfo.InvariantCulture; info = info.Parent)
                {
                    counters = GetPerformanceCounterLib(machine, info).GetCounters(category, ref categoryExists);
                    if (categoryExists)
                    {
                        return(counters);
                    }
                }
            }
            if (!categoryExists)
            {
                throw new InvalidOperationException(SR.GetString("MissingCategory"));
            }
            return(counters);
        }