/// <summary>
        ///     Intializes required resources
        /// </summary>
        private void InitializeImpl()
        {
            var tookLock = false;

            try
            {
                Monitor.Enter(InstanceLockObject, ref tookLock);

                if (!_initialized)
                {
                    if (CategoryName == string.Empty)
                    {
                        throw new InvalidOperationException(SR.Format(SR.CategoryNameMissing));
                    }
                    if (CounterName == string.Empty)
                    {
                        throw new InvalidOperationException(SR.Format(SR.CounterNameMissing));
                    }

                    if (!PerformanceCounterLib.CounterExists(CategoryName, CounterName))
                    {
                        throw new InvalidOperationException(SR.Format(SR.CounterExists, CategoryName, CounterName));
                    }

                    var categoryType = PerformanceCounterLib.GetCategoryType(CategoryName);
                    if (categoryType == PerformanceCounterCategoryType.MultiInstance)
                    {
                        if (string.IsNullOrEmpty(InstanceName))
                        {
                            throw new InvalidOperationException(SR.Format(SR.MultiInstanceOnly, CategoryName));
                        }
                    }
                    else if (categoryType == PerformanceCounterCategoryType.SingleInstance)
                    {
                        if (!string.IsNullOrEmpty(InstanceName))
                        {
                            throw new InvalidOperationException(SR.Format(SR.SingleInstanceOnly, CategoryName));
                        }
                    }

                    if (_instanceLifetime != PerformanceCounterInstanceLifetime.Global)
                    {
                        throw new InvalidOperationException(SR.Format(SR.InstanceLifetimeProcessonReadOnly));
                    }

                    _initialized = true;
                }
            }
            finally
            {
                if (tookLock)
                {
                    Monitor.Exit(InstanceLockObject);
                }
            }
        }
Exemple #2
0
        /// <summary>
        ///     Returns true if the counter is registered for this category
        /// </summary>
        public bool CounterExists(string counterName)
        {
            if (counterName == null)
            {
                throw new ArgumentNullException(nameof(counterName));
            }

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

            return(PerformanceCounterLib.CounterExists(_categoryName, counterName));
        }
Exemple #3
0
        /// <summary>
        ///     Returns true if the counter is registered for this category on a particular machine.
        /// </summary>
        public static bool CounterExists(string counterName, string categoryName)
        {
            if (counterName == null)
            {
                throw new ArgumentNullException(nameof(counterName));
            }

            if (categoryName == null)
            {
                throw new ArgumentNullException(nameof(categoryName));
            }

            if (categoryName.Length == 0)
            {
                throw new ArgumentException(SR.Format(SR.InvalidParameter, nameof(categoryName), categoryName), nameof(categoryName));
            }

            return(PerformanceCounterLib.CounterExists(categoryName, counterName));
        }