public void Bootstrap(PerfCounterSetup perfCounterSetup) { try { foreach (var category in perfCounterSetup.CounterSetupCategories) { //load any existing perf metrics so we dont destroy them var existingValues = GetPreservedValues(category.CounterCategoryName, category.PerfCounterSetupItems.Select(x => x.CounterName).ToArray()); //delete the category try { PerformanceCounterCategory.Delete(category.CounterCategoryName); } catch (InvalidOperationException) { } //rebuild the category var counterCreationDataCollection = new CounterCreationDataCollection(category.PerfCounterSetupItems.Select(x => x.GetCounterCreationData()).ToArray()); PerformanceCounterCategory.Create(category.CounterCategoryName, category.CounterCategoryDescription, PerformanceCounterCategoryType.SingleInstance, counterCreationDataCollection); //load the perf counters into memory and restore any pre-existing values from before they were rebuilt foreach (var perfCounterSetupItem in category.PerfCounterSetupItems) { var c = new PerformanceCounter(); c.CategoryName = category.CounterCategoryName; c.CounterName = perfCounterSetupItem.CounterName; // c.InstanceName = GetInstanceName(); c.ReadOnly = false; // c.InstanceLifetime = PerformanceCounterInstanceLifetime.Process; // c.RawValue = 0; if (existingValues.ContainsKey(perfCounterSetupItem.CounterName)) { c.RawValue = existingValues[perfCounterSetupItem.CounterName]; } PerformanceCounters[category.CounterCategoryName + "." + perfCounterSetupItem.CounterName] = c; } } } catch (SecurityException exp) { throw new SecurityException(string.Format("Error encountered in bootstrap (setting up performance counters): [{0}]. This may be fixed by running in elevated privileges.", exp.Message)); } }
public void Bootstrap(PerfCounterSetup perfCounterSetup) { PerformanceCounters = new Dictionary <string, PerformanceCounter>(); }