Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to enumerate performance objects using ASP.net?

Posted on 2002-07-10
18
Medium Priority
?
751 Views
Last Modified: 2012-06-21
I would like to create an app similar to PerfMon.
To do so, i need to be able to enumerate the Performance Objects available on my local machine, as well as the list of Counters for a particular object and the list of instances.

How do i get these info using ASP.NEt and C# on a webform?
Any examples will be greatly appreciated.

Thanks.
0
Comment
Question by:moloko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 5
18 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 7144698
There is a Perfmon sample in MSDN entitled "PerfMon Sample: Demonstrates How to Monitor System Performance Using Performance Counters".  The sample is written in C#.
0
 

Author Comment

by:moloko
ID: 7144748
Thanks.  The article and help on MSDN seem to tell me how to get counters based on a CategoryName or performance object.
I would like to enumerate the list of performance objects (CategoryNames).  Any examples on how to do that?
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 800 total points
ID: 7147098
Sample from MSDN:

// C#
private void btnGetCategories_Click(object sender, System.EventArgs e)
{
   PerformanceCounterCategory[] myCat2;
   // Clear the list's current contents.
   this.listBox2.Items.Clear();
   // Retrieve the categories.
   myCat2 = PerformanceCounterCategory.GetCategories();
   // Add the retrieved categories to the list.
   for (int i = 0; i < myCat2.Length; i++)
   {
      this.listBox2.Items.Add(myCat2[i].CategoryName);
   }
}



Sample uses a Windows Form with a button and listbox on it.  You must also import the System.Diagnostics namespace at the top of your code module.
0
How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

 
LVL 23

Expert Comment

by:naveenkohli
ID: 7147143
PerformanceCounterCategory[] catList = PerformanceCounterCategory.GetCategories();
               Trace.WriteLine(catList.Length);
               foreach (PerformanceCounterCategory cat in catList)
               {
                    Trace.WriteLine(cat.CategoryName);
                    PerformanceCounter [] counters = cat.GetCounters();
                    foreach (PerformanceCounter counter in counters)
                    {
                         Trace.WriteLine("\t" + counter.CounterName);
                    }
               }
0
 

Author Comment

by:moloko
ID: 7147632
Thanks a lot.
Thanks to Naveen too but Azra helped first.

0
 

Author Comment

by:moloko
ID: 7147772
Would you happen to know what this error msg means?

"Counter is not single instance, an instance name needs to be specified"

I get that when i do a cat[i].GetCounters()
it only happens for certain categories.

thanks in advance.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7148974
Certain categories have multiple instances.  Call the GetInstanceNames method of the PerformanceCounterCategory object first to get all of the instances, and then alter your line to read:

cat[i].GetCounters(strInstance)

for each instance found for that category.
0
 

Author Comment

by:moloko
ID: 7149289
Yes thanks a lot.
But some categories do not return any instances info.
I am getting the following error for some:

"The IAS Authentication Clients category doesn't provide any instance information, no accurate data can be returned. "
0
 

Author Comment

by:moloko
ID: 7149323
Yes thanks a lot.
But some categories do not return any instances info.
I am getting the following error for some:

"The IAS Authentication Clients category doesn't provide any instance information, no accurate data can be returned. "
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7149336
What happens if you just call GetCounters() on that particular category?
0
 

Author Comment

by:moloko
ID: 7151902
Hi,

I tried your suggestion and am getting the same error.
"The IAS Authentication Clients category doesn't provide any instance information, no accurate data can be returned"

thanks.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7152420
I guess that is something you should check for in your try...catch block.  Certain categories may not provide any information.
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7153221
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7153224
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7153225
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7153226
moloko,
The exceptions you are getting are perfectly normal and valid. You just can't enumerate the counters in a catagory like that. There is more to it. GetCounters only works if there is a single instance of a category. But there are categories that have multiple instances. For those you will always get ArguementExceptions if you call GetCOunters(). For example "Thread" category. Try calling GetCounters() for this one. You will get exception thrown. The reason is that there are lots of threads running in the system. And each thread is a separate instance. Although the category will remain same for Thread type, but instances are different. In that case you will have to call GetCOunters (myInstanceName) by providing the instance name as parameter.
For categories like "System", there are no multiple instances. So you can directly enumerate the counters.
You can experiement it yourself by instantiating "PerfMon" and then choose "System" category and the choose "Thread" category. You will see that in first case "Instances" list is empty and disabled. Whereas for Thread, the Instances list is populated with all threads currently  running in the system.

0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7153231
The way to enumerate counters and other info for multi instance categories is...
1. Call GetCOunters() within try/catch.
2. Catch the ArgumentException exception.
3. In this exception handler, call ReadCategoty method. This will return you InstanceDataCollectionCollection.
4. This collection contains list of InstanceDataCollection.
5. Enumerate each entry in this list. And each item conatains InstanceData objects. Use these objects to get instance names and counter values.

0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7153232
Following is small snippet of code that will demo all this..
***********************
private void GetCounterCategories()
          {
               PerformanceCounterCategory[] catList;
               try
               {
                    catList = PerformanceCounterCategory.GetCategories();
                    int iCatCount = catList.Length;
                    Trace.WriteLine("There are " + iCatCount.ToString() + " categories");
                   
                    foreach (PerformanceCounterCategory cat in catList)
                    {
                         Trace.WriteLine(cat.CategoryName);
                         try
                         {
                              PerformanceCounter[] counters = cat.GetCounters();
                         }
                         catch(ArgumentException ex)
                         {
                              Trace.WriteLine(ex.Message);
                              InstanceDataCollectionCollection col = cat.ReadCategory();
                              DumpInstanceDataCollectionCollection(col);
                         }
                    }
               }
               catch (Win32Exception ex)
               {
                    Trace.WriteLine(ex.Message);
               }
          }

          private void DumpInstanceDataCollectionCollection(InstanceDataCollectionCollection col)
          {
               ICollection keys = col.Keys;
               foreach (object key in keys)
               {
                    object val = col[key.ToString()];
                    Trace.WriteLine("\t" + key.ToString() + ":" + val.ToString());
                    if (val is InstanceDataCollection)
                    {
                         DumpInstanceDataCollection((InstanceDataCollection)val);
                    }
               }
          }

          private void DumpInstanceDataCollection(InstanceDataCollection col)
          {
               ICollection keys = col.Keys;
               foreach (object key in keys)
               {
                    object val = col[key.ToString()];
                    Trace.WriteLine("\t" + key.ToString() + ":" + val.ToString());
                    if (val is InstanceData)
                    {
                         DumpInstance((InstanceData)val);
                    }
               }
          }

          private void DumpInstance(InstanceData data)
          {
               Trace.WriteLine("Data: " + data.InstanceName);
          }
***********************

Hope it helps..

Naveen
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question