public static List <PerformanceCounterCategoryModel> GetPerformanceCounterCategories() { var counterCategories = new List <PerformanceCounterCategoryModel>(); PerformanceCounterCategory.GetCategories().ToList().ForEach((i) => counterCategories.Add(new PerformanceCounterCategoryModel { Name = i.CategoryName }) ); counterCategories = counterCategories.OrderBy(x => x.Name).ToList(); counterCategories.ForEach((i) => { try { var cat = new PerformanceCounterCategory(i.Name); var instances = cat.GetInstanceNames(); if (instances.Length > 0) { foreach (var instance in instances) { i.Instances.Add(instance); } } } catch { // sometimes this freaks out when an instance can't be examined } }); return(counterCategories); }
private void CreateCounters() { try { PerformanceCounterCategory[] categories = PerformanceCounterCategory.GetCategories(); foreach (PerformanceCounterCategory category in categories) { if (category.CategoryName == "Processor") { string[] instances = category.GetInstanceNames(); foreach (string instance in instances) { AddCounter(new PerformanceCounter(category.CategoryName, "% Processor Time", instance)); } } } AddCounter(new PerformanceCounter("Memory", "Available KBytes")); } catch (UnauthorizedAccessException) { WriteLine("Not authorized to get counters"); } catch (Exception e) { WriteLine("Error getting counters: {0}", e); } }
public List <string> CategoryNames(out string ex) { ex = string.Empty; PerformanceCounterCategory[] myCategories = null; List <string> performanceCounterCategories = new List <string>(); string node = Environment.MachineName.ToUpperInvariant(); try { //get all the categories on the system myCategories = PerformanceCounterCategory.GetCategories(node); foreach (var performanceCounterCategory in myCategories) { performanceCounterCategories.Add(performanceCounterCategory.CategoryName.ToString()); } List <string> categoriesInOrder = new List <string>() { }; categoriesInOrder = performanceCounterCategories.OrderBy(c => c).ToList(); return(categoriesInOrder); } catch (Exception e) { ex = e.ToString(); return(performanceCounterCategories); } }
private static void PrintPerfCounterCategories() { var categories = PerformanceCounterCategory.GetCategories(); foreach (var category in categories) { Console.WriteLine(string.Format("Name {0}, Type {1}", category.CategoryName, category.CategoryName)); //var instances = category.GetInstanceNames(); //if (instances == null || instances.Count() == 0) //{ // foreach (var counter in category.GetCounters()) // { // Console.WriteLine(string.Format(" Counter {0}", counter.CounterName)); // } //} //else //{ // foreach(var instance in instances) // { // Console.WriteLine(string.Format(" Instance {0}", instance)); // foreach (var counter in category.GetCounters(instance)) // { // Console.WriteLine(string.Format(" Counter {0}", counter.CounterName)); // } // } //} Console.WriteLine(); } }
private static void _indexerBackgroundWorker_DoWork(object sender, DoWorkEventArgs workerEventArgs) { var worker = (BackgroundWorker)sender; // TODO maybe we should use an RAMDirectory to store the index on memory...); using (var indexer = new Indexer("index")) { var categories = PerformanceCounterCategory.GetCategories(); foreach (var n in categories.Select((e, i) => new{ e, i })) { var category = n.e; var counters = category.CategoryType == PerformanceCounterCategoryType.SingleInstance ? category.GetCounters() : category.GetCounters(""); foreach (var counter in counters) { indexer.AddPerformanceCounter(counter); counter.Dispose(); } if (worker.CancellationPending) { workerEventArgs.Cancel = true; return; } worker.ReportProgress(n.i * 100 / categories.Length); } } }
public static string GetProcessInfo() { var info = new StringBuilder(); var categories = PerformanceCounterCategory.GetCategories(); foreach (var category in categories.Where(c => c.CategoryName.StartsWith(".NET") && c.CategoryType == PerformanceCounterCategoryType.SingleInstance)) { try { info.AppendLine(category.CategoryName); // Console.WriteLine(category.CategoryName); foreach (var counter in category.GetCounters(Process.GetCurrentProcess().ProcessName)) { try { info.Append('\t'); info.AppendLine($"{counter.CounterName} : {counter.NextValue():N}"); // Console.WriteLine("\t" + counter.CounterName); } catch (Exception ex) { // Console.WriteLine(xe); } } } catch (Exception ex) { // Console.WriteLine(ex); } } return(info.ToString()); }
public void SetASPNETCategoryNames() { PerformanceCounterCategory[] arrCategories = PerformanceCounterCategory.GetCategories(); // DataTable dt1 = dtPCID.DefaultView.ToTable(true, "PObjectName"); string strCategory; for (int index = 0; index < dtPCID.Rows.Count; index++) { strCategory = dtPCID.Rows[index]["PObjectName"].ToString(); if (strCategory.Contains("ASP.NET Apps")) { dtPCID.Rows[index].BeginEdit(); foreach (PerformanceCounterCategory pcy in arrCategories) { if (pcy.CategoryName.Contains(strCategory)) { dtPCID.Rows[index]["PObjectName"] = pcy.CategoryName; } } dtPCID.Rows[index].EndEdit(); dtPCID.AcceptChanges(); } } }
public static void GetInstanceAndCounterListByAppName() { foreach (var cat in PerformanceCounterCategory.GetCategories()) { try { string[] instances = cat.GetInstanceNames(); if (instances.Length > 0) { Console.WriteLine("Category Name: " + cat.CategoryName); foreach (string instance in instances) { if (instance == _appName) { Console.WriteLine(" Instance: " + instance); if (cat.InstanceExists(instance)) { foreach (PerformanceCounter ctr in cat.GetCounters(instance)) { Console.WriteLine(" Counter: " + ctr.CounterName + ", RawValue: " + ctr.RawValue + ", NextValue: " + ctr.NextValue()); } } } } } } catch { } } }
/// <summary> /// This method will be collecting statistics constantly /// </summary> public void CollectCounter() { if (!IsRunning) { IsRunning = true; //List<MetricInfo> infos = new List<MetricInfo>(); //// get the counters you want to monitor - sql and processor var processorCategory = PerformanceCounterCategory.GetCategories() .FirstOrDefault(cat => cat.CategoryName == "Processor"); var countersInCategory = processorCategory.GetCounters("_Total"); var sqlServerMemoryCategory = PerformanceCounterCategory.GetCategories() .FirstOrDefault(cat => cat.CategoryName == "SQLServer:Memory Manager"); PerformanceCounter sqlPerformanceCounter = sqlServerMemoryCategory.GetCounters().First(cnt => cnt.CounterName.StartsWith("Total Server ")); PerformanceCounter processorPerformanceCounter = countersInCategory.First(cnt => cnt.CounterName == "% Processor Time"); PerformanceCounter[] performanceCounters = new PerformanceCounter[] { sqlPerformanceCounter, processorPerformanceCounter }; LogWriter collectWriter = new LogWriter(ReportDirectory); while (true) { foreach (PerformanceCounter performanceCounter in performanceCounters) { string miStr = string.Format("{3}\t{0}\t{1}\t{2}", performanceCounter.CategoryName, performanceCounter.CounterName, performanceCounter.NextValue(), DateTime.Now.ToString()); collectWriter.WriteToFile(miStr); } System.Threading.Thread.Sleep(3000); //// collect every 3 seconds because it is constantly collecting } IsRunning = false; } }
private IEnumerable <PerformanceCounter> GetAllCpuLoadPerformanceCounters() { var processorCategory = PerformanceCounterCategory.GetCategories().Single(x => x.CategoryName == "Prozessor"); return(processorCategory.GetInstanceNames() .SelectMany(instanceName => processorCategory.GetCounters(instanceName))); }
public CategoryList List() { #if DEBUG Console.WriteLine("List()"); #endif if (!categories.Any()) { foreach (var category in PerformanceCounterCategory.GetCategories()) { if (!allowedCategories.Contains(category.CategoryName)) { continue; } List <Counter> counters = new List <Counter>(); foreach (var counter in category.GetCounters(string.Empty)) { counters.Add(new Counter() { Name = counter.CounterName }); } categories.Add(new Category() { Name = category.CategoryName, Counters = counters }); } } return(categories); }
public static List <string> Info_CategoryNames() { var categories = PerformanceCounterCategory.GetCategories(); var result = categories.Select(x => x.CategoryName).ToList(); return(result); }
static void LW_SprawdzanieDostepnychLicznikow() { PerformanceCounterCategory[] cats = PerformanceCounterCategory.GetCategories(); foreach (var cat in cats) { Console.WriteLine("Kategoria: " + cat.CategoryName); string[] instances = cat.GetInstanceNames(); if (instances.Length == 0) { foreach (var ctr in cat.GetCounters()) { Console.WriteLine(" Licznik: " + ctr.CounterName); } } else { foreach (var instance in instances) { Console.WriteLine(" Egzemplarz: " + instance); if (cat.InstanceExists(instance)) { foreach (var ctr in cat.GetCounters(instance)) { Console.WriteLine(" Licznik: " + ctr.CounterName); } } } } } }
static void LW_LicznikWydajnosciNET() { var x = new XElement("counters", from PerformanceCounterCategory cat in PerformanceCounterCategory.GetCategories() where cat.CategoryName.StartsWith(".NET") let instances = cat.GetInstanceNames() select new XElement("category", new XAttribute("name", cat.CategoryName), instances.Length == 0 ? from c in cat.GetCounters() select new XElement("counter", new XAttribute("name", c.CounterName)) : from i in instances select new XElement("instance", new XAttribute("name", i), !cat.InstanceExists(i) ? null : from c in cat.GetCounters(i) select new XElement("counter", new XAttribute("name", c.CounterName)) ) ) ); Console.WriteLine(x); }
public static bool SetupCategoryAndCounters() { bool created = false; if (PerformanceCounterCategory.Exists(PerformanceCounterConfiguration.CategoryName)) { _counterCategory = PerformanceCounterCategory.GetCategories() .First(c => c.CategoryName == PerformanceCounterConfiguration.CategoryName); } else { var counters = new List <CounterCreationData>(); foreach (var item in PerformanceCounterConfiguration.GetData()) { counters.Add(new CounterCreationData(item.Name, item.Desc, item.Kind)); } _counterCategory = PerformanceCounterCategory.Create( PerformanceCounterConfiguration.CategoryName, PerformanceCounterConfiguration.CategoryDesc, PerformanceCounterCategoryType.MultiInstance, new CounterCreationDataCollection(counters.ToArray())); Console.WriteLine("Created the following performance counters:"); counters.ForEach(c => Console.WriteLine($" > {c.CounterName} ({c.CounterHelp}) [{c.CounterType}]")); Console.WriteLine(""); created = true; } return(created); }
public void RemoveInstancesAndDispose_MultiInstanceCounters_Works(int numOfCounters) { // Arrange var helpers = new List <CounterHelper <MultiInstanceCategory> >(); for (int i = 0; i < numOfCounters; i++) { var instanceName = GetInstanceName(i); helpers.Add(PerformanceHelper.CreateCounterHelper <MultiInstanceCategory>(instanceName)); } //Act helpers.ForEach(helper => helper.RemoveInstancesAndDispose()); // Assert for (int i = 0; i < numOfCounters; i++) { // The counters still exist Assert.IsTrue(PerformanceCounterCategory.CounterExists(AvgCounterName, CategoryName)); Assert.IsTrue(PerformanceCounterCategory.CounterExists(NumCounterName, CategoryName)); // The instances should NOT Assert.IsFalse( PerformanceCounterCategory.InstanceExists(GetInstanceName(i), CategoryName), string.Format("{0} should not exist anymore", GetInstanceName(i))); var category = PerformanceCounterCategory.GetCategories().First(cat => cat.CategoryName == CategoryName); var instanceNames = category.GetInstanceNames().OrderBy(name => name).ToArray(); Assert.AreEqual(0, instanceNames.Count(), "There should be no instances left"); } }
public static void OutputDebugData() { var categories = PerformanceCounterCategory.GetCategories(); var category = categories.Where(c => c.CategoryName == PerformanceCounterConfiguration.CategoryName).FirstOrDefault(); if (category != null) { Console.WriteLine($"Getting counters for {category.CategoryName}.."); if (category.CategoryType == PerformanceCounterCategoryType.MultiInstance) { Console.Write($"Getting instances.."); List <string> instanceNames = category.GetInstanceNames().ToList(); instanceNames.ForEach(i => Console.Write($" {i}")); Console.WriteLine(""); if (instanceNames.Count() > 0) { foreach (string instance in instanceNames) { Console.WriteLine($"Instance counters for '{instance}':"); var lines = category.GetCounters(instance).Select(x => $" > {x.CounterName}: {GetRawValue(x)}").ToList(); lines.ForEach(c => Console.WriteLine(c)); } } else { Console.WriteLine($"No instances within {category.CategoryName}"); } } Console.WriteLine($"Finished getting counters for {category.CategoryName}"); Console.WriteLine(""); } }
/// <summary> /// Gets all the counters from a single category /// </summary> /// <param name="categoryName">string of the Category Name</param> public static void GetAllCountersForACategory(string categoryName) { PerformanceCounterCategory[] perfCats = PerformanceCounterCategory.GetCategories(); PerformanceCounterCategory cat = perfCats.FirstOrDefault(c => c.CategoryName == categoryName); if (cat != null) { Console.WriteLine("Category Name: {0}", cat.CategoryName); string[] instances = cat.GetInstanceNames(); if (instances.Length == 0) { foreach (PerformanceCounter counter in cat.GetCounters()) { Console.WriteLine(" Counter Name: {0}", counter.CounterName); } } else { foreach (string instance in instances) { Console.WriteLine(" Instance Name: {0}", instance); if (cat.InstanceExists(instance)) { foreach (PerformanceCounter counter in cat.GetCounters(instance)) { Console.WriteLine(" Counter Name: {0}", counter.CounterName); } } } } } }
private static void EnumerateCounters() { var categories = PerformanceCounterCategory.GetCategories().Select(c => c.CategoryName).OrderBy(s => s).ToArray(); var sb = new StringBuilder(); foreach (var category in categories) { var counterCategory = new PerformanceCounterCategory(category); foreach (var counterInstance in counterCategory.GetInstanceNames()) { try { foreach (var counter in counterCategory.GetCounters(counterInstance)) { sb.AppendLine(string.Format("{0}:{1}:{2}", counterInstance, category, counter.CounterName)); } } catch { // Drop it on the floor } } } Console.WriteLine(sb.ToString()); }
private void Initialize() { try { if (PerformanceCounterCategory.Exists(CastleWindsorCategoryName)) { Debug.Assert(PerformanceCounterCategory.GetCategories().Count(c => c.CategoryName == CastleWindsorCategoryName) == 1); } else { CreateWindsorCategoryAndCounters(); } } catch (Win32Exception e) { exception = e; } catch (UnauthorizedAccessException e) { exception = e; } catch (SecurityException e) { // it's not in the documentation but PerformanceCounterCategory.Create can also throw SecurityException, exception = e; } catch (InvalidOperationException e) { // it's not in the documentation but PerformanceCounterCategory.Exists can also throw InvalidOperationException // according to this StackOverflow question when PerformanceCounters file is corrupted // http://stackoverflow.com/questions/1540777/performancecounters-on-net-4-0-windows-7 exception = e; } }
static void Main(string[] args) { var categories = PerformanceCounterCategory.GetCategories(); foreach (var cat in categories) { if (cat.CategoryType != PerformanceCounterCategoryType.MultiInstance) { Console.WriteLine("Category: " + cat.CategoryName); foreach (var counter in cat.GetCounters()) { Console.WriteLine("Counter: " + counter.CounterName + ": " + counter.NextSample().RawValue); } } else //if (cat.CategoryType == PerformanceCounterCategoryType.MultiInstance) { foreach (var instance in cat.GetInstanceNames()) { Console.WriteLine("Instance: " + instance); foreach (var counter in cat.GetCounters(instance)) { try { Console.WriteLine("Counter: " + counter.CounterName + ": " + counter.NextSample().RawValue); } catch { // swallow exceptions for counter that require a set base. } } } } } Console.ReadLine(); }
public PerfCounterIgnoreFactAttribute(string categoryName) { if (!PerformanceCounterCategory.GetCategories().Any(x => x.CategoryName == categoryName)) { Skip = $"Please install {categoryName} Performance Counter category to run."; } }
public static IList <PerformanceCounterCategory> GetDotNetPerformanceCategories() { var result = PerformanceCounterCategory.GetCategories().Where(c => c.CategoryName.StartsWith(".Net", StringComparison.OrdinalIgnoreCase)) .ToList(); return(result); }
/// <summary> /// 获取本机所有计数器类型 /// </summary> public static List <string> GetAllCategorys() { List <PerformanceCounterCategory> pcc = PerformanceCounterCategory.GetCategories().ToList(); List <string> result = pcc.Select(t => t.CategoryName).Distinct().ToList(); return(result); }
private void InitPerformance() { downloadCounters = new List <PerformanceCounter>(); uploadCounters = new List <PerformanceCounter>(); PerformanceCounterCategory[] pcc = PerformanceCounterCategory.GetCategories(); foreach (PerformanceCounterCategory p in pcc) { if (p.CategoryName == "Network Interface") { foreach (string instanceName in p.GetInstanceNames()) { if (CheckInterfaceName(instanceName)) { PerformanceCounter[] counters = p.GetCounters(instanceName); foreach (PerformanceCounter pc in counters) { if (pc.CounterName == "Bytes Received/sec") { downloadCounters.Add(pc); } else if (pc.CounterName == "Bytes Sent/sec") { uploadCounters.Add(pc); } } } } } } }
public static List <PerformanceCounter> ListCounters(string[] categoryNames) { List <PerformanceCounter> lst_perfCounter = new List <PerformanceCounter>(); foreach (string categoryName in categoryNames) { PerformanceCounter[] temp_pc = null; PerformanceCounterCategory category = PerformanceCounterCategory.GetCategories().First(c => c.CategoryName == categoryName); // Console.WriteLine("{0} [{1}]", category.CategoryName, category.CategoryType); string[] instanceNames = category.GetInstanceNames(); if (instanceNames.Length > 0) { // MultiInstance categories foreach (string instanceName in instanceNames) { temp_pc = ListInstances(category, instanceName); } } else { // SingleInstance categories temp_pc = ListInstances(category, string.Empty); } lst_perfCounter.AddRange(temp_pc); } return(lst_perfCounter); }
private void MainForm_Load(object sender, EventArgs e) { var categories = PerformanceCounterCategory.GetCategories().OrderBy(c => c.CategoryName); treeView.BeginUpdate(); try { foreach (var category in categories) { var node = new TreeNode(category.CategoryName) { Tag = new CategoryInfo { Name = category.CategoryName, Help = category.CategoryHelp, Type = category.CategoryType } }; node.Nodes.Add("Loading..."); treeView.Nodes.Add(node); } } finally { treeView.EndUpdate(); } }
private void Initialize() { try { if (PerformanceCounterCategory.Exists(CastleWindsorCategoryName)) { var categories = PerformanceCounterCategory.GetCategories(); categories.Single(c => c.CategoryName == CastleWindsorCategoryName); } else { CreateWindsorCategoryAndCounters(); } } catch (Win32Exception e) { exception = e; } catch (UnauthorizedAccessException e) { exception = e; } // it's not in the documentation but PerformanceCounterCategory.Create can also throw SecurityException, catch (SecurityException e) { exception = e; } }
public List <string> GetCounterNames(string categoryName, string instanceName) { PerformanceCounterCategory[] myCategories = PerformanceCounterCategory.GetCategories(); List <string> performanceCounterNames = new List <string>(); foreach (var performanceCounterCategory in myCategories.Where(r => r.CategoryName == categoryName)) { PerformanceCounter[] counters = null; if (instanceName == null) { counters = performanceCounterCategory.GetCounters(); } else { counters = performanceCounterCategory.GetCounters(instanceName.ToString()); } foreach (var performanceCounter in counters) { performanceCounterNames.Add(performanceCounter.CounterName.ToString()); } } List <string> countersInOrder = new List <string>() { }; countersInOrder = performanceCounterNames.OrderBy(c => c).ToList(); return(countersInOrder); }
public void Load() { var categories = PerformanceCounterCategory.GetCategories() .Select(t => new CategoryViewModel(t)); _categories.Load(categories); }