通过PerformanceCounter计算异常

时间:2018-04-19 13:13:22

标签: c# exception performancecounter

我试图计算程序中引发的异常总数。 PerformanceCounter的设置看起来正确,但它提供的输出没有任何意义。

这是我写的:

PerformanceCounter pc = new PerformanceCounter();
pc.CategoryName = ".NET CLR Exceptions";
pc.CounterName = "# of Exceps Thrown";
pc.InstanceName = "_Global_";
pc.BeginInit();

for (int i = 0; i < 5; i++)
{
    try
    {
        throw new Exception();
    }
    catch (Exception)
    {
        Console.WriteLine("Exception!");
    }
}
pc.EndInit();
Console.WriteLine("CounterName: " + pc.CounterName);
Console.WriteLine("CounterHelp: " + pc.CounterHelp);
Console.WriteLine("CounterType: " + pc.CounterType);
Console.WriteLine("RawValue: " + pc.RawValue);

这是控制台输出:

Exception!
Exception!
Exception!
Exception!
Exception!
CounterName: # of Exceps Thrown
CounterHelp: This counter displays the total number of exceptions thrown since t
he start of the application. These include both .NET exceptions and unmanaged ex
ceptions that get converted into .NET exceptions e.g. null pointer reference exc
eption in unmanaged code would get re-thrown in managed code as a .NET System.Nu
llReferenceException; this counter includes both handled and unhandled exception
s. Exceptions that are re-thrown would get counted again. Exceptions should only
 occur in rare situations and not in the normal control flow of the program.
CounterType: NumberOfItems32
RawValue: 38790

我已经做到这一点,总共抛出五个异常,从控制台输出来看,它可以正常工作。另外,CounterHelp表明理论上这个计数器应该产生我正在寻找的结果。

但是,我似乎无法检索PerformanceCounter所计算的实际数量的例外情况。 38790看起来有点高。 (当代码多次运行时,此数字会发生变化。)

我做错了什么?我的做法是正确的,还是我完全咆哮了错误的树?

0 个答案:

没有答案