2010-02-19 3 views
13

다른 프로세스를 시작하고 실행 프로세스의 프로세서 사용량을 확인하는 C# 응용 프로그램에 성능 카운터를 프로그래밍하려고합니다. 성능 카운터 클래스를 사용하면 범주 이름, 카운터 이름 및 프로세스 이름을 할당해야합니다. 나는 프로세스 이름을 충분히 쉽게 얻을 수 있지만 할당 할 수있는 모든 범주와 카운터 이름을 가진 인터넷에 일종의 목록이 있습니까? 이런 식으로 MSDN을 샅샅이 조사했지만 아무 것도 찾지 못했습니다.성능 카운터 범주 이름?

모든 도움에 감사드립니다!

답변

18

나는 프로세스의 어떤 부분을 모니터링 할 수 있는지 알고 싶습니다. 프로세스 성능 카운터 목록 사용 가능 here 정적 방법 GetCategories 정적 방법을 사용하여 시스템의 모든 범주를 나열하거나 좀 더 구체적으로 "프로세스"범주의 PerformanceCategory를 만들고 GetCounters을 사용하여 목록을 가져올 수 있습니다 사용 가능한 모든 카운터 중 희망이 도움이됩니다.

+3

이 클래스는 사용하기 매우 혼란입니다! 왜 그들은 많은 복잡한 문자로 구성된 문자열 대신 enum을 사용하지 않았습니까? – TheGateKeeper

+1

제 생각에 모든 제품 팀 (Windows, IIS 등)이 카운터 이름을 "소유"한다는 사실에 근거하여 모든 시점에서 언제든지 이름을 추가/제거/변경할 수 있습니다. 이 방법 외에 우리 모두는 우리 자신의 카운터 세트를 만들 수 있습니다. – CriGoT

+0

사용자 지정 데이터를 모니터링하는 카운터를 만들지 않아도 프로그래밍 방식으로 수행 할 수 있습니다. – TheGateKeeper

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

도움이되는 도구에 대한 감사의 말을 전하고 싶지만이 문제에 약간의 문제가 있음을 알았습니다. 특히 영어 이외의 다른 언어를 사용하는 컴퓨터에서는 제대로 작동하지 않습니다. 나는 메모리에 대한 인스턴스와 카운터를 얻으려고했지만 내 언어로 된 다른 것에서부터 빈 목록을 얻는다. 이 문제를 설명하기 위해 코드를 편집 할 수 있습니까? –

+0

헤이 @ 사이몬 젠슨, 머리를 가져 주셔서 감사합니다. 나는 시스템에서 카테고리 목록을 얻고 있는데, 목록을 반환하지 않는다는 것이 상당히 이상하게 보입니다.이 예제에서는 특정 언어가 없습니다. 하지만 저는 이중 언어를 사용하기 때문에 시스템 언어를 변경하고 테스트해볼 수 있습니다. 나는 자유 시간을 가질 때이 문제를 찾으려고 노력할 것이다. – n1kk