2010-02-19 75 views
13

我試圖在性能計數器到我的C#應用​​程序,啓動另一個進程,並檢查啓動過程的處理器使用編程。據我瞭解,性能計數器類要求我分配一個類別名稱,一個計數器名稱和一個進程名稱。我可以很容易地得到進程名稱,但是在互聯網上是否有一個列表有我可以分配的所有可能的類別和計數器名稱?我試圖爲MSDN搜索這樣的東西,但我沒有找到任何東西。性能計數器類別名稱? (C#)

感謝您的幫助!

回答

18

我想你想知道你可以監視的過程的哪些方面。過程性能計數器列表可用here 儘管如此,您仍然可以使用靜態方法列出機器中的所有類別,或者您可以更具體地爲「過程」類別創建PerformanceCategory,並使用GetCounters來獲取列表的所有可用計數器。 希望這有助於。

+3

這個類是如此混亂使用!他們爲什麼不使用枚舉而不是由許多複雜字符組成的字符串! – TheGateKeeper 2012-04-16 21:28:45

+1

我的猜測是,它基於每個產品團隊(Windows,IIS等)「擁有」計數器名稱的事實,因此他們可以隨時添加/刪除/更改任何名稱。 除此之外,我們都可以創建自己的計數器。 – CriGoT 2012-04-18 13:04:47

+0

我認爲沒有必要創建一個計數器來監控自定義數據,你可以通過編程來完成。 – TheGateKeeper 2012-04-18 16:17:43

1

您可以將它們分配任何你想要的。性能監視器將簡單地顯示您選擇的任何類別以及您爲特定需求選擇的任何計數器名稱。

CounterCreationDataCollection ccdc = new CounterCreationDataCollection(); 
ccdc.Add(new CounterCreationData("Counter Title", "Counter Description", PerformanceCounterType.NumberOfItems32)); 
PerformanceCounterCategory.Create("My Counter Category", "Category Description", PerformanceCounterCategoryType.Unknown, ccdc); 
2

我已經創建了顯示什麼CriGoT在上面創作的,一個小的快捷方式的方法。

private static void GetAllCounters(string categoryFilter) 
    { 
     var categories = PerformanceCounterCategory.GetCategories(); 
     foreach (var cat in categories) 
     { 
      if (categoryFilter != null && categoryFilter.Length > 0) 
      { 
       if (!cat.CategoryName.Contains(categoryFilter)) continue; 
      } 
      Console.WriteLine("Category {0}", cat.CategoryName); 
      try 
      { 
       var instances = cat.GetInstanceNames(); 
       if (instances != null && instances.Length > 0) 
       { 
        foreach (var instance in instances) 
        { 
         //if (cat.CounterExists(instance)) 
         //{ 
          foreach (var counter in cat.GetCounters(instance)) 
          { 
           Console.WriteLine("\tCounter Name {0} [{1}]", counter.CounterName, instance); 
          } 
         //} 
        } 
       } 
       else 
       { 
        foreach (var counter in cat.GetCounters()) 
        { 
         Console.WriteLine("\tCounter Name {0}", counter.CounterName); 
        } 
       } 
      } 
      catch (Exception) 
      { 
       // NO COUNTERS 
      } 
     } 
     Console.ReadLine(); 
} 

希望得到這個幫助。

1

對於那些誰想要快速瀏覽和查找所需的反這裏有一個快速表單顯示三個列表框與|Categories|Instances|Counters|和被一個計時器更新計數器值。帶過濾器。

enter image description here

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.Linq; 
using System.Windows.Forms; 

namespace CountersListPreview 
{ 
    internal static class CounterPreview 
    { 
     [STAThread] 
     private static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 

      Form f = new CountersListPreviewForm(); 
      Application.Run(f); 
     } 
    } 

    internal class CountersListPreviewForm : Form 
    { 
     public CountersListPreviewForm() 
     { 
      InitializeComponent(); 
     } 

     private PerformanceCounterCategory[] allCats; 
     private string[] instances; 
     private PerformanceCounter[] counters; 
     private PerformanceCounter counter; 
     private Timer TitleRefreshTimer; 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      allCats = PerformanceCounterCategory.GetCategories(); 
      listBox1.DataSource = allCats; 
      listBox1.DisplayMember = "CategoryName"; 

      listBox1.SelectedIndexChanged += On_CatChange; 
      listBox2.SelectedIndexChanged += On_InstChange; 
      listBox3.SelectedIndexChanged += On_CounterChange; 

      textBox2.TextChanged += On_CatFilterChanged; 
      textBox3.TextChanged += On_InstFilterChanged; 
      textBox4.TextChanged += On_CounterFilterChanged; 

      TitleRefreshTimer = new Timer(); 
      TitleRefreshTimer.Tick += On_Timer; 
      TitleRefreshTimer.Interval = 500; 
      TitleRefreshTimer.Start(); 
     } 

     private void On_Timer(object sender, EventArgs e) 
     { 
      textBox1.Text = counter != null ? counter.NextValue().ToString() : ""; 
     } 

     // --------------- SELECTION CHANGE ------------------ 

     private void On_CatChange(object sender, EventArgs e) 
     { 
      var cat = listBox1.SelectedItem as PerformanceCounterCategory; 
      listBox2.DataSource = instances = cat.GetInstanceNames(); 
     } 

     private void On_InstChange(object sender, EventArgs e) 
     { 
      var cat = listBox1.SelectedItem as PerformanceCounterCategory; 
      var inst = listBox2.SelectedItem as string; 
      listBox3.DataSource = counters = cat.GetCounters(inst); 
      listBox3.DisplayMember = "CounterName"; 
     } 

     private void On_CounterChange(object sender, EventArgs e) 
     { 
      counter = listBox3.SelectedItem as PerformanceCounter; 
      On_Timer(null, null); 
     } 

     // --------------- FILTERS ------------------ 

     private void On_CatFilterChanged(object sender, EventArgs e) 
     { 
      var filter = textBox2.Text; 
      listBox1.DataSource = !string.IsNullOrEmpty(filter) 
       ? allCats.Where(cat => cat.CategoryName.ToLower().Contains(filter.ToLower())).ToArray() 
       : allCats; 
     } 

     private void On_InstFilterChanged(object sender, EventArgs e) 
     { 
      var filter = textBox3.Text; 
      listBox2.DataSource = !string.IsNullOrEmpty(filter) 
       ? instances.Where(inst => inst.ToLower().Contains(filter.ToLower())).ToArray() 
       : instances; 
     } 

     private void On_CounterFilterChanged(object sender, EventArgs e) 
     { 
      var filter = textBox4.Text; 
      listBox3.DataSource = !string.IsNullOrEmpty(filter) 
       ? counters.Where(c => c.CounterName.ToLower().Contains(filter.ToLower())).ToArray() 
       : counters; 
     } 

     // --------------- FORM AND LAYOUT ------------------ 

     private readonly IContainer components = null; 

     protected override void Dispose(bool disposing) 
     { 
      if (disposing && components != null) components.Dispose(); 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     private void InitializeComponent() 
     { 
      this.listBox1 = new System.Windows.Forms.ListBox(); 
      this.listBox2 = new System.Windows.Forms.ListBox(); 
      this.listBox3 = new System.Windows.Forms.ListBox(); 
      this.textBox1 = new System.Windows.Forms.TextBox(); 
      this.label1 = new System.Windows.Forms.Label(); 
      this.textBox2 = new System.Windows.Forms.TextBox(); 
      this.textBox3 = new System.Windows.Forms.TextBox(); 
      this.textBox4 = new System.Windows.Forms.TextBox(); 
      this.SuspendLayout(); 
      // 
      // listBox1 
      // 
      this.listBox1.FormattingEnabled = true; 
      this.listBox1.Location = new System.Drawing.Point(12, 38); 
      this.listBox1.Name = "listBox1"; 
      this.listBox1.Size = new System.Drawing.Size(351, 524); 
      this.listBox1.TabIndex = 3; 
      // 
      // listBox2 
      // 
      this.listBox2.FormattingEnabled = true; 
      this.listBox2.Location = new System.Drawing.Point(369, 38); 
      this.listBox2.Name = "listBox2"; 
      this.listBox2.Size = new System.Drawing.Size(351, 524); 
      this.listBox2.TabIndex = 3; 
      // 
      // listBox3 
      // 
      this.listBox3.FormattingEnabled = true; 
      this.listBox3.Location = new System.Drawing.Point(726, 38); 
      this.listBox3.Name = "listBox3"; 
      this.listBox3.Size = new System.Drawing.Size(351, 524); 
      this.listBox3.TabIndex = 3; 
      // 
      // textBox1 
      // 
      this.textBox1.Location = new System.Drawing.Point(726, 568); 
      this.textBox1.Name = "textBox1"; 
      this.textBox1.Size = new System.Drawing.Size(351, 20); 
      this.textBox1.TabIndex = 4; 
      // 
      // label1 
      // 
      this.label1.AutoSize = true; 
      this.label1.Location = new System.Drawing.Point(606, 571); 
      this.label1.Name = "label1"; 
      this.label1.Size = new System.Drawing.Size(114, 13); 
      this.label1.TabIndex = 5; 
      this.label1.Text = "Counter Value (500ms)"; 
      // 
      // textBox2 
      // 
      this.textBox2.Location = new System.Drawing.Point(12, 12); 
      this.textBox2.Name = "textBox2"; 
      this.textBox2.Size = new System.Drawing.Size(351, 20); 
      this.textBox2.TabIndex = 4; 
      // 
      // textBox3 
      // 
      this.textBox3.Location = new System.Drawing.Point(369, 12); 
      this.textBox3.Name = "textBox3"; 
      this.textBox3.Size = new System.Drawing.Size(351, 20); 
      this.textBox3.TabIndex = 4; 
      // 
      // textBox4 
      // 
      this.textBox4.Location = new System.Drawing.Point(726, 12); 
      this.textBox4.Name = "textBox4"; 
      this.textBox4.Size = new System.Drawing.Size(351, 20); 
      this.textBox4.TabIndex = 4; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      //this.BackColor = System.Drawing.SystemColors.; 
      this.ClientSize = new System.Drawing.Size(1090, 597); 
      this.Controls.Add(this.label1); 
      this.Controls.Add(this.textBox4); 
      this.Controls.Add(this.textBox3); 
      this.Controls.Add(this.textBox2); 
      this.Controls.Add(this.textBox1); 
      this.Controls.Add(this.listBox3); 
      this.Controls.Add(this.listBox2); 
      this.Controls.Add(this.listBox1); 
      //this.ForeColor = System.Drawing.SystemColors.ControlLightLight; 
      this.Name = "Form1"; 
      this.Load += new System.EventHandler(this.Form1_Load); 
      this.ResumeLayout(false); 
      this.PerformLayout(); 
     } 

     #endregion 

     private ListBox listBox1; 
     private ListBox listBox2; 
     private ListBox listBox3; 
     private TextBox textBox1; 
     private Label label1; 
     private TextBox textBox2; 
     private TextBox textBox3; 
     private TextBox textBox4; 
    } 
} 
+0

我只想說謝謝這個有用的工具,但是我發現它有點問題,它在使用與英語不同的語言的計算機上無法正常工作,特別是針對類別。我試圖獲取內存的實例和計數器,但是因爲它用我的語言調用了其他東西,所以我只是爲這些列表獲得了一個空列表。你能編輯代碼來解釋這個嗎? – 2017-10-13 13:30:44

+0

嗨@SimonJensen,謝謝你的擡頭。我從系統中得到一個類別列表,似乎很奇怪它不會返回列表,在這個例子中我沒有任何特定的語言。但我也是雙語的,所以我可以改變我的系統語言並進行測試。當我有空閒時,我會盡力找到問題。 – n1kk 2017-10-16 07:34:53