Beispiel #1
0
        /// <summary>
        /// Internal Constructor for named instances (multi-instance counters)
        /// </summary>
        /// <param name="instanceName">name for this instance</param>
        /// <param name="categoryInfo">information about this category</param>
        /// <param name="enumCounterAttributes">enumerator attributes</param>
        /// <exception cref="System.NotSupportedException" />
        internal CounterHelperImpl(string instanceName, PerformanceCounterCategoryAttribute categoryInfo, Dictionary <T, PerformanceCounterAttribute> enumCounterAttributes)
            : this(enumCounterAttributes.Count, instanceName)
        {
            if ((categoryInfo.InstanceType == PerformanceCounterCategoryType.MultiInstance) &&
                (string.IsNullOrEmpty(instanceName)))
            {
                throw new NotSupportedException(Properties.Resources.CounterHelper_MultiInstanceNoInstanceNameErrorMessage);
            }

            PerformanceCounter performanceCounter, performanceCounterBase = null;

            Dictionary <T, PerformanceCounterAttribute> .Enumerator enumerator = enumCounterAttributes.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if (categoryInfo.InstanceType == PerformanceCounterCategoryType.MultiInstance)
                {
                    performanceCounter = new PerformanceCounter(categoryInfo.Name,
                                                                enumerator.Current.Value.Name,
                                                                this._instanceName,
                                                                false);
                }
                else
                {
                    performanceCounter = new PerformanceCounter(categoryInfo.Name,
                                                                enumerator.Current.Value.Name,
                                                                false);
                }
                PerformanceCounterType?baseType = PerformanceHelper.GetBaseType(performanceCounter.CounterType);

                if (baseType != null)
                {
                    if (categoryInfo.InstanceType == PerformanceCounterCategoryType.MultiInstance)
                    {
                        performanceCounterBase = new PerformanceCounter(categoryInfo.Name,
                                                                        PerformanceHelper.GetCounterNameForBaseType(enumerator.Current.Value.Name),
                                                                        instanceName,
                                                                        false);
                    }
                    else
                    {
                        performanceCounterBase = new PerformanceCounter(categoryInfo.Name,
                                                                        PerformanceHelper.GetCounterNameForBaseType(enumerator.Current.Value.Name),
                                                                        false);
                    }
                    performanceCounterBase.RawValue = PerformanceHelper.getInitialValue(performanceCounter.CounterType);
                }
                else
                {
                    performanceCounterBase = null;
                }

                PerformanceCounterContainer performanceCounterContainer = new PerformanceCounterContainer(performanceCounter, performanceCounterBase, enumerator.Current.Value.IsBaseAutoIncreased);
                this._counters.Add(enumerator.Current.Key, performanceCounterContainer);
            }
        }