// Will cause an update, raw value
        /// <summary>
        ///     Obtains a counter sample and returns the raw value for it.
        /// </summary>
        public CounterSample NextSample()
        {
            Initialize();


            using (var categorySample = PerformanceCounterLib.GetCategorySample(CategoryName))
            {
                var counterSample = categorySample.GetCounterDefinitionSample(CounterName);
                _counterType = counterSample._counterType;
                if (!categorySample._isMultiInstance)
                {
                    if (!string.IsNullOrEmpty(InstanceName))
                    {
                        throw new InvalidOperationException(SR.Format(SR.InstanceNameProhibited, InstanceName));
                    }

                    return(counterSample.GetSingleValue());
                }

                if (string.IsNullOrEmpty(InstanceName))
                {
                    throw new InvalidOperationException(SR.Format(SR.InstanceNameRequired));
                }

                return(counterSample.GetInstanceValue(InstanceName));
            }
        }
Exemple #2
0
        /// <summary>
        ///     Reads all the counter and instance data of this performance category.  Note that reading the entire category
        ///     at once can be as efficient as reading a single counter because of the way the system provides the data.
        /// </summary>
        public InstanceDataCollectionCollection ReadCategory()
        {
            if (_categoryName == null)
            {
                throw new InvalidOperationException(SR.Format(SR.CategoryNameNotSet));
            }

            using (var categorySample = PerformanceCounterLib.GetCategorySample(_categoryName))
            {
                return(categorySample.ReadCategory());
            }
        }
Exemple #3
0
        /// <summary>
        ///     Returns true if the instance already exists for this category.
        /// </summary>
        public bool InstanceExists(string instanceName)
        {
            if (instanceName == null)
            {
                throw new ArgumentNullException(nameof(instanceName));
            }

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

            using (var categorySample = PerformanceCounterLib.GetCategorySample(_categoryName))
            {
                return(categorySample._instanceNameTable.ContainsKey(instanceName));
            }
        }
Exemple #4
0
        /// <summary>
        ///     Returns the instance names for a given category
        /// </summary>
        /// <internalonly/>
        internal static string[] GetCounterInstances(string categoryName)
        {
            using (var categorySample = PerformanceCounterLib.GetCategorySample(categoryName))
            {
                if (categorySample._instanceNameTable.Count == 0)
                {
                    return(Array.Empty <string>());
                }

                var instanceNames = new string[categorySample._instanceNameTable.Count];
                categorySample._instanceNameTable.Keys.CopyTo(instanceNames, 0);
                if (instanceNames.Length == 1 && instanceNames[0] == PerformanceCounterLib.SingleInstanceName)
                {
                    return(Array.Empty <string>());
                }

                return(instanceNames);
            }
        }