How do I change the category of performance counters?

I have a performance counter category. The counters for this category may change for my next version, so when the program starts I want to check if the category exists and it is the correct version - if not, create a new category. I can do this by storing the GUID in the help line, but it's clearly smelly. Can this be done more cleanly with the .NET API?

The existing smelly version ...

if (PerformanceCounterCategory.Exists(CATEGORY_NAME))
{
    PerformanceCounterCategory c = new PerformanceCounterCategory(CATEGORY_NAME);
    if (c.CategoryHelp != CATEGORY_VERSION)
    {
        PerformanceCounterCategory.Delete(CATEGORY_NAME);
    }
}

if (!PerformanceCounterCategory.Exists(CATEGORY_NAME))
{
      // Create category
}

      

+2


source to share


2 answers


In our system, every time an application is launched, we check for an existing category. If not found, we create a category. If it exists, we compare the existing category with what we expect and recreate (delete, create) if values ​​are missing.



var missing = counters
.Where(counter => !PerformanceCounterCategory.CounterExists(counter.Name, CategoryName))
.Count();

if (missing > 0)
{
    PerformanceCounterCategory.Delete(CategoryName);

    PerformanceCounterCategory.Create(
        CategoryName,
        CategoryHelp,
        PerformanceCounterCategoryType.MultiInstance,
        new CounterCreationDataCollection(counters.Select(x => (CounterCreationData)x).ToArray()));
}

      

+3


source


I don't think there is a better way. IMHO, I don't think this is a terrible solution.



0


source







All Articles