2010-02-19 16 views
13

मैं अपने सी # एप्लिकेशन में एक प्रदर्शन काउंटर में प्रोग्राम करने की कोशिश कर रहा हूं जो एक और प्रक्रिया शुरू करता है और उस लॉन्च प्रक्रिया के प्रोसेसर उपयोग की जांच करता है। जैसा कि मैं इसे समझता हूं, प्रदर्शन काउंटर क्लास के लिए मुझे एक श्रेणी का नाम, काउंटर नाम और एक प्रक्रिया नाम असाइन करने की आवश्यकता होती है। मैं प्रक्रिया का नाम आसानी से प्राप्त कर सकता हूं, लेकिन क्या इंटरनेट पर किसी प्रकार की एक सूची है जिसमें सभी संभावित श्रेणी और काउंटर नाम हैं जिन्हें मैं असाइन कर सकता हूं? मैंने इस तरह के कुछ के लिए एमएसडीएन scouring करने की कोशिश की, लेकिन मुझे कुछ भी नहीं मिला।प्रदर्शन काउंटर श्रेणी नाम? (सी #)

सभी मदद के लिए धन्यवाद!

उत्तर

18

मुझे लगता है कि आप जानना चाहते हैं कि प्रक्रिया के कौन से पहलू आप निगरानी कर सकते हैं। प्रक्रिया प्रदर्शन काउंटर की एक सूची here उपलब्ध है फिर भी आप मशीन में सभी श्रेणियों को सूचीबद्ध करने के लिए GetCategories स्थैतिक विधि का उपयोग कर सकते हैं या आप अधिक विशिष्ट हो सकते हैं और "प्रक्रिया" श्रेणी के लिए प्रदर्शन श्रेणी बना सकते हैं और सूची प्राप्त करने के लिए GetCounters का उपयोग कर सकते हैं सभी काउंटर उपलब्ध हैं। उम्मीद है कि यह मदद करता है।

+3

इस वर्ग तो उपयोग करने के लिए भ्रामक है! उन्होंने इतने सारे जटिल पात्रों से युक्त तारों के बजाय enums का उपयोग क्यों नहीं किया! – TheGateKeeper

+1

मेरा अनुमान है कि यह इस तथ्य पर आधारित है कि प्रत्येक उत्पाद टीम (विंडोज़, आईआईएस, आदि) काउंटर नामों का "मालिक" है इसलिए वे किसी भी समय किसी भी नाम को जोड़/हटा/बदल सकते हैं। इस तरह से हम सभी काउंटरों का अपना सेट बना सकते हैं। – 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

मैंने एक तरीका बनाया है जो दिखाता है कि क्रियोगेट ने ऊपर एक छोटा शॉर्टकट क्या लिखा है।

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

संबंधित मुद्दे