内存PerformanceCounter C#

问题描述

我在C#中遇到PerformanceCounter的麻烦。当我在远程计算机上运行它时,它运行良好,但是当尝试在它运行的计算机上运行它时,出现以下错误 system.invalidOperationException:'无法找到具有指定类别名称'Memory'的性能计数器,计数器名称为“使用中的%承诺字节”。”

            int i = 1;
            PerformanceCounter ramPerfCounter = new PerformanceCounter();
            ramPerfCounter.CategoryName = "Memory";
            ramPerfCounter.CounterName = "% Committed Bytes In Use";
            ramPerfCounter.MachineName = "server";
            ramPerfCounter.ReadOnly = true;

            PerformanceCounter cpuPerfCounter = new PerformanceCounter();
            cpuPerfCounter.CategoryName = "Processor";
            cpuPerfCounter.CounterName = "% Processor Time";
            cpuPerfCounter.InstanceName = "_Total";
            cpuPerfCounter.MachineName = "server";
            cpuPerfCounter.ReadOnly = true;

            do
            {

                float fMemory = ramPerfCounter.NextValue();
                float fcpu = cpuPerfCounter.NextValue();

                Console.WriteLine("cpu: " + fcpu.ToString());
                Console.WriteLine("RAM:" + fMemory.ToString());
                Thread.Sleep(1000);
            }
            while (i == 1);

cpu性能计数器工作正常。

解决方法

可能有几个问题

  1. 您无需为在本地实例上运行而指定MachineName,它用于连接到只读远程实例。
  2. 计数器名称可以在本地计算机上本地化

要检查计数器在您的计算机上是否可用

  1. 在资源管理器中转到“控制面板\所有控制面板项目\管理工具”
  2. 打开性能监视器
  3. 在“监视工具->性能监视器”面板上,单击+按钮以添加新计数器
  4. 从列表中单击选择内存,然后查看可用的计数器。

或者,您可以使用此代码获取类别列表

public static List<string> GetCategoryNames(string machineName)
{
    List<string> catNameList = new List<string>();
    List<PerformanceCounterCategory> categories = new List<PerformanceCounterCategory>();
    try
    {
        if (!string.IsNullOrEmpty(machineName))
        {
            categories = PerformanceCounterCategory.GetCategories(machineName).ToList();
        }
        else
        {
            categories = PerformanceCounterCategory.GetCategories().ToList();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    foreach(PerformanceCounterCategory category in categories)
    {
        catNameList.Add(category.CategoryName);
    }
    catNameList.Sort();
    return catNameList;
}

然后添加catNameList.Contains()调用以检查计数器是否可用。

要查找所有可以使用的计数器

public static Dictionary<string,List<string>> GetPerfCounterNames(string machineName)
{
    Dictionary<string,List<string>> perfCounterList = new Dictionary<string,List<string>>();
    List<PerformanceCounterCategory> categories = new List<PerformanceCounterCategory>();
    try
    {
        if (!string.IsNullOrEmpty(machineName))
        {
            categories = PerformanceCounterCategory.GetCategories(machineName).ToList();
        }
        else
        {
            categories = PerformanceCounterCategory.GetCategories().ToList();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    foreach (PerformanceCounterCategory category in categories)
    {
        List<PerformanceCounter> pcList = null;
        if (category.GetInstanceNames().Length > 0)
        {
            pcList = category.GetCounters(category.GetInstanceNames()[0]).ToList();
        }
        else
        {
            pcList = category.GetCounters().ToList();
        }
        List<string> pcNameList = new List<string>();
        foreach (PerformanceCounter pc in pcList)
        {
            pcNameList.Add(pc.CounterName);
        }
        pcNameList.Sort();
        perfCounterList.Add(category.CategoryName,pcNameList);
    }
    return perfCounterList;
}