moloko
asked on
how to enumerate performance objects using ASP.net?
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.
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.
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#.
ASKER
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?
I would like to enumerate the list of performance objects (CategoryNames). Any examples on how to do that?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
PerformanceCounterCategory [] catList = PerformanceCounterCategory .GetCatego ries();
Trace.WriteLine(catList.Le ngth);
foreach (PerformanceCounterCategor y cat in catList)
{
Trace.WriteLine(cat.Catego ryName);
PerformanceCounter [] counters = cat.GetCounters();
foreach (PerformanceCounter counter in counters)
{
Trace.WriteLine("\t" + counter.CounterName);
}
}
Trace.WriteLine(catList.Le
foreach (PerformanceCounterCategor
{
Trace.WriteLine(cat.Catego
PerformanceCounter [] counters = cat.GetCounters();
foreach (PerformanceCounter counter in counters)
{
Trace.WriteLine("\t" + counter.CounterName);
}
}
ASKER
Thanks a lot.
Thanks to Naveen too but Azra helped first.
Thanks to Naveen too but Azra helped first.
ASKER
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.
"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.
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(strInst ance)
for each instance found for that category.
cat[i].GetCounters(strInst
for each instance found for that category.
ASKER
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. "
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. "
ASKER
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. "
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. "
What happens if you just call GetCounters() on that particular category?
ASKER
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.
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.
I guess that is something you should check for in your try...catch block. Certain categories may not provide any information.
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.
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.
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 InstanceDataCollectionColl ection.
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.
1. Call GetCOunters() within try/catch.
2. Catch the ArgumentException exception.
3. In this exception handler, call ReadCategoty method. This will return you InstanceDataCollectionColl
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.
Following is small snippet of code that will demo all this..
***********************
private void GetCounterCategories()
{
PerformanceCounterCategory [] catList;
try
{
catList = PerformanceCounterCategory .GetCatego ries();
int iCatCount = catList.Length;
Trace.WriteLine("There are " + iCatCount.ToString() + " categories");
foreach (PerformanceCounterCategor y cat in catList)
{
Trace.WriteLine(cat.Catego ryName);
try
{
PerformanceCounter[] counters = cat.GetCounters();
}
catch(ArgumentException ex)
{
Trace.WriteLine(ex.Message );
InstanceDataCollectionColl ection col = cat.ReadCategory();
DumpInstanceDataCollection Collection (col);
}
}
}
catch (Win32Exception ex)
{
Trace.WriteLine(ex.Message );
}
}
private void DumpInstanceDataCollection Collection (InstanceD ataCollect ionCollect ion 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 ((Instance DataCollec tion)val);
}
}
}
private void DumpInstanceDataCollection (InstanceD ataCollect ion 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
***********************
private void GetCounterCategories()
{
PerformanceCounterCategory
try
{
catList = PerformanceCounterCategory
int iCatCount = catList.Length;
Trace.WriteLine("There are " + iCatCount.ToString() + " categories");
foreach (PerformanceCounterCategor
{
Trace.WriteLine(cat.Catego
try
{
PerformanceCounter[] counters = cat.GetCounters();
}
catch(ArgumentException ex)
{
Trace.WriteLine(ex.Message
InstanceDataCollectionColl
DumpInstanceDataCollection
}
}
}
catch (Win32Exception ex)
{
Trace.WriteLine(ex.Message
}
}
private void DumpInstanceDataCollection
{
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
}
}
}
private void DumpInstanceDataCollection
{
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
}
}
}
private void DumpInstance(InstanceData data)
{
Trace.WriteLine("Data: " + data.InstanceName);
}
***********************
Hope it helps..
Naveen