我目前正在寻找一种使用.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); …Run Code Online (Sandbox Code Playgroud) PerformanceCounter 类通常用于通过获取 CPU 使用率和内存来分析进程。
但是,PerformanceCounter 在 Linux 中不起作用。我无法将我的项目从 .NET Core 更改为 .NET 标准或 .NET 框架
我已经查看了这篇文章:如何在 .Net Core 中获取进程的 CPU 使用率和虚拟内存?.
但是我不能在我的工作中使用 Mono。
一些网站(https://stackify.com/performance-counters-net-core/)显示 .NET Core 中的 EventCounter 可以完成这项工作,但另一个论坛https://gitter.im/dotnet/coreclr/archives/2017/ 02/08已确认它不起作用。
如何在不使用 PerformanceCounter 的情况下测量 .NET Core 中的 CPU 使用率和内存?