/// <summary> /// Harvest a performance category. /// </summary> /// <param name="category">The name of the performance category.</param> /// <returns>A harvested file.</returns> public Util.PerformanceCategory HarvestPerformanceCategory(string category) { if (null == category) { throw new ArgumentNullException("category"); } if (PerformanceCounterCategory.Exists(category)) { Util.PerformanceCategory perfCategory = new Util.PerformanceCategory(); // Get the performance counter category and set the appropriate WiX attributes PerformanceCounterCategory pcc = PerformanceCounterCategory.GetCategories().Single(c => string.Equals(c.CategoryName, category)); perfCategory.Id = this.Core.CreateIdentifierFromFilename(pcc.CategoryName); perfCategory.Name = pcc.CategoryName; perfCategory.Help = pcc.CategoryHelp; if (PerformanceCounterCategoryType.MultiInstance == pcc.CategoryType) { perfCategory.MultiInstance = Util.YesNoType.yes; } // If it's multi-instance, check if there are any instances and get counters from there; else we get // the counters straight up. For multi-instance, GetCounters() fails if there are any instances. If there // are no instances, then GetCounters(instance) can't be called since there is no instance. Instances // will exist for each counter even if only one of the counters was "intialized." string[] instances = pcc.GetInstanceNames(); bool hasInstances = instances.Length > 0; PerformanceCounter[] counters = hasInstances ? pcc.GetCounters(instances.First()) : pcc.GetCounters(); foreach (PerformanceCounter counter in counters) { Util.PerformanceCounter perfCounter = new Util.PerformanceCounter(); // Get the performance counter and set the appropriate WiX attributes perfCounter.Name = counter.CounterName; perfCounter.Type = CounterTypeToWix(counter.CounterType); perfCounter.Help = counter.CounterHelp; perfCategory.AddChild(perfCounter); } return(perfCategory); } else { throw new WixException(UtilErrors.PerformanceCategoryNotFound(category)); } }
private Util.PerformanceCategory GetPerformanceCategory(string categoryName) { Console.WriteLine($"Getting counters for CategoryName: {categoryName}"); try { var category = PerformanceCounterCategory.GetCategories() .Single(c => string.Equals(categoryName, c.CategoryName, StringComparison.OrdinalIgnoreCase)); var isMultiInstance = category.CategoryType == PerformanceCounterCategoryType.MultiInstance; Trace.WriteLine($"Found category={categoryName}, MultiInstance={isMultiInstance}"); // If it's multi-instance, check if there are any instances and get counters from there; else we get // the counters straight up. For multi-instance, GetCounters() fails if there are any instances. If there // are no instances, then GetCounters(instance) can't be called since there is no instance. Instances // will exist for each counter even if only one of the counters was "intialized." var hasInstances = category.GetInstanceNames().Length > 0; var counters = hasInstances ? category.GetCounters(category.GetInstanceNames().First()) : category.GetCounters(); Trace.WriteLine($"Found {counters.Length} counters"); var result = new Util.PerformanceCategory { Id = CompilerCore.GetIdentifierFromName(category.CategoryName), Name = category.CategoryName, Help = category.CategoryHelp, MultiInstance = isMultiInstance ? Util.YesNoType.yes : Util.YesNoType.no }; foreach (var counter in counters) { Console.WriteLine($"Counter={counter.CounterName}, Type={counter.CounterType}"); result.AddChild(new Util.PerformanceCounter { Name = counter.CounterName, Type = CounterTypeToWix(counter.CounterType), Help = counter.CounterHelp, }); } return(result); } catch (Exception ex) { Trace.WriteLine(ex.Message); throw new WixException(UtilErrors.PerformanceCategoryNotFound(categoryName)); } }
/// <summary> /// Harvest a performance category. /// </summary> /// <param name="category">The name of the performance category.</param> /// <returns>A harvested file.</returns> public Util.PerformanceCategory HarvestPerformanceCategory(string category) { if (null == category) { throw new ArgumentNullException("category"); } if (PerformanceCounterCategory.Exists(category)) { Util.PerformanceCategory perfCategory = new Util.PerformanceCategory(); // Get the performance counter category and set the appropriate WiX attributes PerformanceCounterCategory pcc = new PerformanceCounterCategory(category); perfCategory.Id = CompilerCore.GetIdentifierFromName(pcc.CategoryName); perfCategory.Name = pcc.CategoryName; perfCategory.Help = pcc.CategoryHelp; if (PerformanceCounterCategoryType.MultiInstance == pcc.CategoryType) { perfCategory.MultiInstance = Util.YesNoType.yes; } foreach (InstanceDataCollection counter in pcc.ReadCategory().Values) { Util.PerformanceCounter perfCounter = new Util.PerformanceCounter(); // Get the performance counter and set the appropriate WiX attributes PerformanceCounter pc = new PerformanceCounter(pcc.CategoryName, counter.CounterName); perfCounter.Name = pc.CounterName; perfCounter.Type = CounterTypeToWix(pc.CounterType); perfCounter.Help = pc.CounterHelp; perfCategory.AddChild(perfCounter); } return(perfCategory); } else { throw new WixException(UtilErrors.PerformanceCategoryNotFound(category)); } }