I have kind of library which uses a bunch of its own perf counters. But I want my library works fine even if that perf counters weren't installed.
So I've created wrappers aroung PerformanceCounter and on the first use check if PerfCounter exists or not. If they exists then I'm using native PerformanceCounter instead I'm using a wrapper which do nothing.
So to check perf counter existence I use PerformanceCounterCategory.Exists
The problem is that if there isn't such category then PerformanceCounterCategory.Exists call takes (on my machine) about 10 seconds! Needless to say it's too slow.
What can I do?
Code to try it by youself: using System; using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
var ts = Stopwatch.StartNew();
var res = PerformanceCounterCategory.Exists("XYZ");
Console.WriteLine(ts.ElapsedMilliseconds);
Console.WriteLine("result:" + res);
}
}