Esempio n. 1
0
        /// <summary>
        /// Создать счётчик определённого типа, если он уже есть в Windows
        /// </summary>
        /// <param name="type">Тип счётчика</param>
        /// <param name="counterName">Имя счётчика</param>
        /// <param name="counterDescription">Описание счётчика</param>
        /// <returns>Счётчик</returns>
        public override Counter CreateCounter(CounterTypes type, string counterName, string counterDescription)
        {
            if (counterName == null)
            {
                throw new ArgumentNullException("counterName");
            }
            if (counterDescription == null)
            {
                throw new ArgumentNullException("counterDescription");
            }

            if (State == WinCategoryState.Disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            Counter res = null;

            if (!_counters.TryGetValue(counterName, out res))
            {
                throw new PerformanceCounterCreationException("Can't create not existed counter in mode 'UseOnlyExisted'. Counter: " + counterName + ", Category: " + this.ToString());
            }

            if (res.Type != type && CounterHelper.IsWinCompatible(res.Type, type))
            {
                var newCntr = CounterHelper.CreateCounter(type, counterName, res.Description, Info);
                if (_counters.TryUpdate(counterName, newCntr, res))
                {
                    (newCntr as IWinCounterInitialization).CounterInit(this.FullName, null);
                    res = newCntr;
                }
            }

            if (res.Type != type)
            {
                throw new PerformanceCounterCreationException("Can't create not existed counter in mode 'UseOnlyExisted'. Counter: " + counterName,
                                                              new InvalidCounterTypeException(string.Format("Counter types are not equal. Expected: {0}, Returned: {1}", type, res.Type)));
            }

            return(res);
        }