c# - 性能计数器丢失

标签 c# windows performancecounter

我的问题是,某些性能计数器类别有时似乎会丢失,我不明白为什么。

下面的代码将抛出异常,如下所示:

查找性能类别失败

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

打印输出包含一长串性能计数器,但不是我想要的。如果我查看 perfmon.exe,我可以一次找到丢失的类别。

我使用以下代码来查找类别内的不同计数器。

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;
    }      
}

问题归结为,是否有某种“魔法”可以让性能计数器消失?或者也许我需要做一些具体的事情?

最佳答案

我遇到了完全相同的问题,这就是我解决它的方法:

服务器架构(x86 或 x64)和 .net (c#) 构建目标必须相同,否则您的代码无法访问所有性能计数器(如果您的构建目标和服务器不同,则只有性能计数器的子集可用) )。

因此,在 Visual Studio 中,右键单击您的项目(在解决方案资源管理器中),然后选择“属性”,然后转到“生成”并选择 x64 作为平台目标(如果您的服务器是 x64,否则选择 x86) .

关于c# - 性能计数器丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14139538/

相关文章:

windows - 如何在 Windows 10 中安装 NASM?

c# - 了解内存性能计数器

c# - 结合 WCF SOAP 和 REST

c# - 无法将类型 bool 隐式转换为 System.Collections.Generic.List

C# - 将对象列表合并为一个对象

c# - FTP over SSL 问题

windows - 如何使用 R 检查字符串是否是 Windows 的有效文件名?

windows - 过滤器驱动程序是否旨在扩展系统驱动程序?

performancecounter - Perf:[<n percent>] 记录在 perf stat 输出中是什么意思?

graph - 使用原始性能计数器数据在 kibana 4 中绘制图表