Beispiel #1
0
        /// <summary>
        /// Создать счётчик определённого типа.
        /// Создаётся только если есть такой в Windows и был найден при создании.
        /// </summary>
        /// <param name="type">Тип счётчика</param>
        /// <param name="counterName">Имя счетчика</param>
        /// <param name="counterDescription">Описание счетчика</param>
        public override void 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);
            }


            WinCounterDescriptor 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.CreateCounterDescriptor(type, counterName, res.Description, Info);
                if (_counters.TryUpdate(counterName, newCntr, res))
                {
                    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)));
            }
        }
        /// <summary>
        /// Создать счётчик определённого типа
        /// </summary>
        /// <param name="type">Тип счётчика</param>
        /// <param name="counterName">Имя счетчика</param>
        /// <param name="counterDescription">Описание счетчика</param>
        public override void 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 (_instances.Count != 0)
            {
                throw new PerformanceCounterCreationException(
                          string.Format("Can't create counter in MultiInstanceCategory ({0}) when it has created instances", 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));
            }


            WinCounterDescriptor res = CounterHelper.CreateCounterDescriptor(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));
            }
        }