.NET使用IResourceMonitor實(shí)現(xiàn)獲取資源信息
寫在前面
在 Microsoft.Extensions.Diagnostics.ResourceMonitoring 包提供了一系列定制 API,專用于監(jiān)視 .NET 應(yīng)用程序的資源利用率。
為了讓控制臺輸出的樣式更美觀,可以安裝一下Spectre.Console這個(gè)包

本例主要通過 IResourceMonitor 來獲取資源狀態(tài)信息,該接口支持檢索與 CPU 和內(nèi)存使用情況相關(guān)的數(shù)據(jù),并且當(dāng)前與 Windows 和 Linux 平臺兼容。

示例代碼中用到的 Microsoft.Extensions.Logging 類庫也需要通過NuGet安裝一下。
代碼實(shí)現(xiàn)
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring;
using Microsoft.Extensions.Logging;
using Spectre.Console;
public class Program
{
public static void Main(string[] args)
{
var services = new ServiceCollection()
.AddLogging()
.AddResourceMonitoring();
var provider = services.BuildServiceProvider();
var monitor = provider.GetRequiredService<IResourceMonitor>();
_ = StartMonitoringAsync(monitor, new CancellationToken());
Console.ReadKey();
}
static async Task StartMonitoringAsync(IResourceMonitor monitor, CancellationToken cancellationToken)
{
var table = new Table()
.Centered()
.Title("Resource Monitoring", new Style(foreground: Color.Purple, decoration: Decoration.Bold))
.Caption("Updates every three seconds. *GTD: Guaranteed ", new Style(decoration: Decoration.Dim))
.RoundedBorder()
.BorderColor(Color.Cyan1)
.AddColumns(
[
new TableColumn("Time").Centered(),
new TableColumn("CPU %").Centered(),
new TableColumn("Memory %").Centered(),
new TableColumn("Memory (bytes)").Centered(),
new TableColumn("GTD / Max Memory (bytes)").Centered(),
new TableColumn("GTD / Max CPU (units)").Centered(),
]);
await AnsiConsole.Live(table)
.StartAsync(async ctx =>
{
var window = TimeSpan.FromSeconds(3);
while (cancellationToken.IsCancellationRequested is false)
{
var utilization = monitor.GetUtilization(window);
var resources = utilization.SystemResources;
table.AddRow(
[
$"{DateTime.Now:T}",
$"{utilization.CpuUsedPercentage:p}",
$"{utilization.MemoryUsedPercentage:p}",
$"{utilization.MemoryUsedInBytes:#,#}",
$"{resources.GuaranteedMemoryInBytes:#,#} / {resources.MaximumMemoryInBytes:#,#}",
$"{resources.GuaranteedCpuUnits} / {resources.MaximumCpuUnits}",
]);
ctx.Refresh();
await Task.Delay(window);
}
});
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
};
}
}
調(diào)用示例

到此這篇關(guān)于.NET使用IResourceMonitor實(shí)現(xiàn)獲取資源信息的文章就介紹到這了,更多相關(guān).NET獲取資源信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)系統(tǒng)信息監(jiān)控與獲取功能
在 C# 開發(fā)的眾多應(yīng)用場景中,獲取系統(tǒng)信息以及監(jiān)控用戶操作有著廣泛的用途,比如在系統(tǒng)性能優(yōu)化工具中,需要實(shí)時(shí)讀取 CPU、GPU 資源信息,本文將詳細(xì)介紹如何使用 C# 來實(shí)現(xiàn)這些功能,助力大家在開發(fā)中更好地與系統(tǒng)底層進(jìn)行交互,需要的朋友可以參考下2025-01-01
C#實(shí)現(xiàn)在窗體上的統(tǒng)計(jì)圖效果
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)在窗體上的統(tǒng)計(jì)圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Unity3D實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲(1)
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
C#并發(fā)編程之a(chǎn)sync和await關(guān)鍵字詳解
對于?async?和?await?兩個(gè)關(guān)鍵字,對于一線開發(fā)人員再熟悉不過了,到處都是它們的身影,下面小編就來和大家記錄匯總下它們的使用吧2023-07-07
C#寫入對象或集合類型數(shù)據(jù)到xml文件的方法
這篇文章主要介紹了C#寫入對象或集合類型數(shù)據(jù)到xml文件的方法,涉及C#針對XML文件的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
C# 7.0之ref locals and returns(局部變量和引用返回)
這篇文章主要介紹了C# 7.0之ref locals and returns,即局部變量和引用返回,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

