/// <summary> /// /// </summary> /// <param name="instanceName"></param> /// <param name="categoryName"></param> /// <param name="machineName"></param> /// <returns></returns> public static PerfmonCounter GetCounterInstance(string instanceName, string categoryName, string machineName) { PerfmonCounter counterInstance = null; string counterKey = string.Format("{0}[{1}.{2}]", instanceName, machineName, categoryName); if (PerfmonCounterMap.ContainsKey(counterKey)) { counterInstance = PerfmonCounterMap[counterKey]; } else { lock (syscObject) { if (PerfmonCounterMap.ContainsKey(counterKey)) { counterInstance = PerfmonCounterMap[counterKey]; } else if (string.IsNullOrEmpty(instanceName) || PerformanceCounterCategory.InstanceExists(instanceName, categoryName, machineName)) { var counterCategory = GetCounterCategory(categoryName, machineName); counterInstance = counterCategory.Instances[instanceName]; PerfmonCounterMap[counterKey] = counterInstance; } } } return(counterInstance); }
/// <summary> /// /// </summary> /// <param name="categoryName"></param> /// <param name="machineName"></param> /// <returns></returns> public static PerfmonCounterCategory GetCounterCategory(string categoryName, string machineName) { if (PerformanceCounterCategory.Exists(categoryName, machineName) == false) { return(null); } var counterCategory = new PerfmonCounterCategory(categoryName, machineName); counterCategory.CategoryName = categoryName; var sysCategory = new PerformanceCounterCategory(categoryName, machineName); var sysInstances = sysCategory.GetInstanceNames(); foreach (var instanceName in sysInstances) { var perfmonCounter = new PerfmonCounter(instanceName, categoryName); var counterItems = sysCategory.GetCounters(instanceName); foreach (var counterItem in counterItems) { perfmonCounter.CounterData.Add(counterItem.CounterName, counterItem); } counterCategory.Instances.Add(instanceName, perfmonCounter); } if (sysInstances.Length == 0) { var categoryCounters = sysCategory.GetCounters(); if (categoryCounters.Length > 0) { var instanceName = string.Empty; var perfmonCounter = new PerfmonCounter(instanceName, categoryName); var counterItems = sysCategory.GetCounters(instanceName); foreach (var counterItem in counterItems) { perfmonCounter.CounterData.Add(counterItem.CounterName, counterItem); } counterCategory.Instances.Add(instanceName, perfmonCounter); } } return(counterCategory); }