How to get current CPU / RAM / Disk usage in a C # web application using .NET CORE?

I'm currently looking for a way to get current CPU / RAM / Disk usage in a C # web application using .NET CORE.

To use the CPU and ram, I use the PerformanceCounter Class from System.Diagnostics. These are the codes:

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

To use the disk, I use the DriveInfo class. These are the codes:

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

Unfortunately, .NET Core does not support the DriveInfo and PerformanceCounter classes, so the codes above do not work.

Does anyone know how I can get the current CPU / RAM / Disk usage in a C # web application using .NET CORE?

+4
source share
1 answer

CPU information is available through 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); 

DriveInfo is available for Core by adding the System.IO.FileSystem.DriveInfo package

+1
source

Source: https://habr.com/ru/post/1270855/


All Articles