/// <summary>
        /// Создать счётчик определённого типа
        /// </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.Created)
            {
                throw new InvalidOperationException("Can't create counter inside initialized category. Category: " + this.ToString());
            }

            if (_counters.ContainsKey(counterName))
            {
                throw new PerformanceCounterCreationException("Counter with the same name is already existed. Name: " + counterName,
                                                              new DuplicateCounterNameException("Counter with the same name is already existed. Name: " + counterName));
            }

            Counter res = CounterHelper.CreateCounter(type, counterName, counterDescription, Info);

            if (!_counters.TryAdd(counterName, res))
            {
                throw new PerformanceCounterCreationException("Counter with the same name is already existed. Name: " + counterName,
                                                              new DuplicateCounterNameException("Counter with the same name is already existed. Name: " + counterName));
            }

            return(res);
        }
예제 #2
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);
        }