2
votes

My problem is that some of the performance counter category seems to go missing sometimes, and I don't understand why.

The code below will throw an exception, like this:

Failed to lookup Performance Category

Error Msg: Category does not exist. CategoryName: HP EVA Physical Disk
Group Category list on target:
ServiceModelService 4.0.
bla bla

Print out contains a long list of performance counters, but not the one I'm after. If I look in perfmon.exe I can find the missing category once.

I'm using the following code to find the different counters inside a category.

public static string[] GetPerformanceCategory(string CategoryName)
{
    //Console.WriteLine("CategoryName to Search for: " + CategoryName);
    if (string.IsNullOrEmpty(CategoryName))
        throw new NullReferenceException("CategoryName is empty");

    try
    {
        PerformanceCounterCategory perfCat = new PerformanceCounterCategory(CategoryName);

        string[] catInstances = perfCat.GetInstanceNames();
        return catInstances;
    }
    catch(Exception Ex)
    {
        StringBuilder ErrorMsg = new StringBuilder();
        ErrorMsg.AppendLine("Failed to lookup Preformance Category");
        ErrorMsg.AppendLine("Error Msg: " + Ex.Message);
        ErrorMsg.AppendLine("CategoryName: " + CategoryName);
        ErrorMsg.AppendLine("Category list on target:");

        StringBuilder CatList = new StringBuilder();
        var categories = PerformanceCounterCategory.GetCategories();
        foreach (var Cat in categories)
            CatList.AppendLine(Cat.CategoryName);                

        ErrorMsg.AppendLine(CatList.ToString());

        Logger.WriteToLog(ErrorMsg.ToString(), EventLogEntryType.Error);
        return null;
    }      
}

The question boils down to, is there some "magic" that can make performance counters disappear? Or perhaps I need to do something specific?

2
Sounds like CategoryName is being set to "HP EVA Physical Disk \nGroup ". Is that correct?lc.
Yes. Thats what i want. But off course no \n ? Was that a copy past error from you?EKS
Sorry I couldn't get the carriage return in the comment. I meant it looks like there's a newline between "Disk" and "Group", if you look at the exception message it looks like there's a newline there. Either way you say it's not showing up in the list, so I guess it doesn't matter...lc.
Could it be something to do with permissions? I know perfmon runs as Administrator.lc.
I have also tried loggin in the application as Domain administrator .EKS

2 Answers

2
votes

I had exactly the same issue, and this is how I fix it:

Server architecture (x86 or x64) and .net (c#) build target must be the same, otherwise your code is unable to access all the performace counters (only a subset of performance counters is available if your build target and server differs).

So in Visual Studio, right-click on your project (in solution explorer), then select "Properties", then go to "Build" and select x64 as platform target (if your server is in x64, otherwise select x86).

1
votes

Being a bit more specific, you can check your Event Viewer and look for PerfLib events.

In my case, they were marked with errors.

Then, after looking around I found the two commands explained above. In my case, the problem was with the perfOS. (you can always run lodctr /q:Perflib to check what's disabled.

Then, run: lodctr /e:perfOS (or change perfOS for whatever service is disabled)

That will do (at least it worked for me)

TLDR: run lodctr /q:perfOs and lodctr /e:perfOS as Admin