/// <summary> /// Installs performance counters in the current assembly using PerfItFilterAttribute. /// </summary> /// /// <param name="categoryName">category name for the metrics. If not provided, it will use the assembly name</param> public static void Install(Assembly installerAssembly, string categoryName = null) { Uninstall(installerAssembly, categoryName); if (string.IsNullOrEmpty(categoryName)) categoryName = installerAssembly.GetName().Name; var perfItFilterAttributes = FindAllFilters(installerAssembly).ToArray(); var counterCreationDataCollection = new CounterCreationDataCollection(); Trace.TraceInformation("Number of filters: {0}", perfItFilterAttributes.Length); foreach (var filter in perfItFilterAttributes) { Trace.TraceInformation("Setting up filters '{0}'", filter.Description); foreach (var counterType in filter.Counters) { if (!HandlerFactories.ContainsKey(counterType)) throw new ArgumentException("Counter type not defined: " + counterType); // if already exists in the set then ignore if (counterCreationDataCollection.Cast<CounterCreationData>().Any(x => x.CounterName == counterType)) { Trace.TraceInformation("Counter type '{0}' was duplicate", counterType); continue; } using (var counterHandler = HandlerFactories[counterType](categoryName, filter.InstanceName)) { counterCreationDataCollection.AddRange(counterHandler.BuildCreationData()); Trace.TraceInformation("Added counter type '{0}'", counterType); } } } PerformanceCounterCategory.Create(categoryName, "PerfIt category for " + categoryName, PerformanceCounterCategoryType.MultiInstance, counterCreationDataCollection); Trace.TraceInformation("Built category '{0}' with {1} items", categoryName, counterCreationDataCollection.Count); }
/// <summary> /// Installs performance counters in the assembly /// </summary> /// <param name="installerAssembly"></param> /// <param name="discoverer">object that can discover aspects inside and assembly</param> /// <param name="categoryName">category name for the metrics. If not provided, it will use the assembly name</param> public static void Install(Assembly installerAssembly, IInstrumentationDiscoverer discoverer, string categoryName = null) { Uninstall(installerAssembly, discoverer, categoryName); if (string.IsNullOrEmpty(categoryName)) categoryName = installerAssembly.GetName().Name; var instrumentationInfos = discoverer.Discover(installerAssembly).ToArray(); if (instrumentationInfos.Length == 0) { throw new InvalidOperationException("There are no instrumentationInfos found by the discoverer!"); } var counterCreationDataCollection = new CounterCreationDataCollection(); Trace.TraceInformation("Number of filters: {0}", instrumentationInfos.Length); foreach (var group in instrumentationInfos.GroupBy(x => x.CategoryName)) { foreach (var instrumentationInfo in group) { Trace.TraceInformation("Setting up filters '{0}'", instrumentationInfo.Description); if (instrumentationInfo.Counters == null || instrumentationInfo.Counters.Length == 0) instrumentationInfo.Counters = CounterTypes.StandardCounters; foreach (var counterType in instrumentationInfo.Counters) { if (!HandlerFactories.ContainsKey(counterType)) throw new ArgumentException("Counter type not defined: " + counterType); // if already exists in the set then ignore if (counterCreationDataCollection.Cast<CounterCreationData>().Any(x => x.CounterName == counterType)) { Trace.TraceInformation("Counter type '{0}' was duplicate", counterType); continue; } using (var counterHandler = HandlerFactories[counterType](categoryName, instrumentationInfo.InstanceName)) { counterCreationDataCollection.AddRange(counterHandler.BuildCreationData()); Trace.TraceInformation("Added counter type '{0}'", counterType); } } } var catName = string.IsNullOrEmpty(group.Key) ? categoryName : group.Key; PerformanceCounterCategory.Create(catName, "PerfIt category for " + catName, PerformanceCounterCategoryType.MultiInstance, counterCreationDataCollection); } Trace.TraceInformation("Built category '{0}' with {1} items", categoryName, counterCreationDataCollection.Count); }
private static bool IsCounterAlreadyIncluded(ref CounterCreationDataCollection colCounterCreationData, string counterName) { return colCounterCreationData.Cast<CounterCreationData>().Any(objCreateCounter => objCreateCounter.CounterName == counterName); }