コード例 #1
0
        private void Add()
        {
            this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Adding Performance Counter Category: {0}", this.CategoryName));
            CounterCreationDataCollection colCounterCreationData = new CounterCreationDataCollection();

            colCounterCreationData.Clear();
            if (PerformanceCounterCategory.Exists(this.CategoryName))
            {
                this.LogTaskMessage(MessageImportance.Low, string.Format(CultureInfo.CurrentCulture, "Removing Performance Counter Category: {0}", this.CategoryName));
                PerformanceCounterCategory.Delete(this.CategoryName);
            }

            foreach (ITaskItem counter in this.CounterList)
            {
                string counterName = counter.GetMetadata("CounterName");
                string counterHelp = counter.GetMetadata("CounterHelp");
                PerformanceCounterType counterType = (PerformanceCounterType)Enum.Parse(typeof(PerformanceCounterType), counter.GetMetadata("CounterType"));
                this.LogTaskMessage(MessageImportance.Low, string.Format(CultureInfo.CurrentCulture, "Adding Performance Counter: {0}", counterName));
                CounterCreationData objCreateCounter = new CounterCreationData(counterName, counterHelp, counterType);
                colCounterCreationData.Add(objCreateCounter);
            }

            if (colCounterCreationData.Count > 0)
            {
                PerformanceCounterCategoryType categoryType = PerformanceCounterCategoryType.SingleInstance;

                if (this.MultiInstance)
                {
                    categoryType = PerformanceCounterCategoryType.MultiInstance;
                }

                this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Creating Performance Counter Category: {0}", this.CategoryName));
                PerformanceCounterCategory.Create(this.CategoryName, this.CategoryHelp, categoryType, colCounterCreationData);
            }
        }