如何使用.NET CORE获取C#Web应用程序中的当前CPU / RAM /磁盘使用情况?

Ant*_*oni 7 c# asp.net-core

我目前正在寻找一种使用.NET CORE在C#Web应用程序中获取当前CPU / RAM /磁盘使用情况的方法。

对于CPU和内存的使用,我使用System.Diagnostics中的PerformanceCounter类。这些是代码:

 PerformanceCounter cpuCounter;
 PerformanceCounter ramCounter;

 cpuCounter = new PerformanceCounter();

cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";

ramCounter = new PerformanceCounter("Memory", "Available MBytes");


public string getCurrentCpuUsage(){
        cpuCounter.NextValue()+"%";
}

public string getAvailableRAM(){
        ramCounter.NextValue()+"MB";
}
Run Code Online (Sandbox Code Playgroud)

对于磁盘使用,我使用DriveInfo类。这些是代码:

 using System;
 using System.IO;

 class Info {
 public static void Main() {
    DriveInfo[] drives = DriveInfo.GetDrives();
    foreach (DriveInfo drive in drives) {
        //There are more attributes you can use.
        //Check the MSDN link for a complete example.
        Console.WriteLine(drive.Name);
        if (drive.IsReady) Console.WriteLine(drive.TotalSize);
    }
  }
 }
Run Code Online (Sandbox Code Playgroud)

不幸的是,.NET Core不支持DriveInfo和PerformanceCounter类,因此上面的代码不起作用。

有谁知道如何使用.NET CORE在C#Web应用程序中获得当前的CPU / RAM /磁盘使用情况?

Wah*_*tar 6

PerformnceCounter您可以在中使用System.Diagnostics.PerformanceCounter

例如,下一个代码将为您提供总处理器使用百分比

var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total",true);
var value = cpuCounter.NextValue();
//Note: In most cases you need to call .NextValue() twice to be able to get the real value
if (Math.Abs(value) <= 0.00)
    value = cpuCounter.NextValue();

Console.WriteLine(value);
Run Code Online (Sandbox Code Playgroud)

您可以对所有操作系统注册的性能计数器执行相同的操作。


更新:

我不确定在创建 PerformanceCounter 类的新实例后是否应该执行某些操作,但有时当我获得下一个值时,它会显示为 0。

因此,我决定在应用程序级别创建一个 PerformanceCounter 实例。

例如

public static class DiagnosticHelpers
{
    static float _systemCPU;
    public static float SystemCPU 
    { 
        get 
        { 
            lock (locker)
            {
                return _systemCPU;
            }
        }
    }

    private static readonly object locker = new object();


    static DiagnosticHelpers()
    {
        SystemCPU = 0;
        Task.Run(() =>
        {
            var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
            cpuCounter.NextValue(); //prime the counter
            while (true)
            {
                Thread.Sleep(1000); /wait at least 1 second before the first real read
                lock (locker)
                {
                    _systemCPU = cpuCounter.NextValue();
                }
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*rev 4

处理器信息可通过以下方式获取System.Diagnostics

var proc = Process.GetCurrentProcess();
var mem = proc.WorkingSet64;
var cpu = proc.TotalProcessorTime;
Console.WriteLine("My process used working set {0:n3} K of working set and CPU {1:n} msec", 
    mem / 1024.0, cpu.TotalMilliseconds);
Run Code Online (Sandbox Code Playgroud)

DriveInfoSystem.IO.FileSystem.DriveInfo通过添加包即可用于 Core

  • 如何获取CPU百分比? (5认同)
  • 另请注意,这是特定于进程的 CPU 使用率,而不是 OP 性能计数器跟踪的系统 CPU 使用率。 (4认同)