Exemplo n.º 1
0
        /// <summary>
        /// Конструктор UseOnlyExistedWinSingleInstanceCategory
        /// </summary>
        /// <param name="name">Имя категории</param>
        /// <param name="description">Описание категории</param>
        /// <param name="rootName">Корневое имя</param>
        /// <param name="info">Информация о функционировании</param>
        internal UseOnlyExistedWinSingleInstanceCategory(string name, string description, string rootName, WinCountersWorkingInfo info)
            : base(name, description, rootName, info)
        {
            if (info.InstantiationMode != WinCountersInstantiationMode.UseOnlyExisted)
            {
                throw new InvalidOperationException("Category UseOnlyExistedWinSingleInstanceCategory can't be used with instantiation mode: " + info.InstantiationMode.ToString());
            }

            if (!HasWinCategory())
            {
                throw new CategoryCreationException(string.Format("Can't create category ({0}) cause it is not registerd in Windows (UseOnlyExisted mode)", this.ToString()));
            }

            _counters = new ConcurrentDictionary <string, Counter>();

            _winCategory = GetExistedWinCategory();
            if (_winCategory.CategoryType != PerformanceCounterCategoryType.SingleInstance)
            {
                throw new CategoryCreationException(string.Format("Can't create category ({0}) cause it's type is not equal to Windows category type ({1}) (UseOnlyExisted mode)", this.ToString(), _winCategory.CategoryType));
            }

            var existedCntr = _winCategory.GetCounters();

            foreach (var cnt in existedCntr)
            {
                var newCounter = CounterHelper.CreateByExistedCounterAndInit(cnt, Info);
                if (newCounter != null)
                {
                    _counters.TryAdd(newCounter.Name, newCounter);
                }

                cnt.Dispose();
            }

            this.Init();
        }