.NET使用IResourceMonitor實(shí)現(xiàn)獲取資源信息
寫在前面
在 Microsoft.Extensions.Diagnostics.ResourceMonitoring 包提供了一系列定制 API,專用于監(jiān)視 .NET 應(yīng)用程序的資源利用率。
為了讓控制臺輸出的樣式更美觀,可以安裝一下Spectre.Console這個包
本例主要通過 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# 基于GMap.NET實(shí)現(xiàn)電子圍欄功能(WPF版)
這篇文章主要介紹了c# 基于GMap.NET實(shí)現(xiàn)電子圍欄功能(WPF版),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03C#中Sleep() 和 Wait()的區(qū)別小結(jié)
Sleep()和 Wait()是兩個不同的方法,用于控制線程的執(zhí)行,本文主要介紹了C#中Sleep()和Wait()的區(qū)別小結(jié),具有一定的參考價值,感興趣的可以了解一下2024-04-04C#?Windows?Forms中實(shí)現(xiàn)控件之間的連接線的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何在C#?Windows?Forms應(yīng)用程序中實(shí)現(xiàn)繪圖工具中多個控件之間的連接線功能,文中的示例代碼講解詳細(xì),需要的可以參考下2024-02-02C# 動態(tài)輸出Dos命令執(zhí)行結(jié)果的實(shí)例(附源碼)
這篇文章主要介紹了C# 動態(tài)輸出Dos命令執(zhí)行結(jié)果的實(shí)例,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07深入解析C#編程中struct所定義的結(jié)構(gòu)
這篇文章主要介紹了C#編程中struct所定義的結(jié)構(gòu),與C++一樣,C#語言同時擁有類和結(jié)構(gòu),需要的朋友可以參考下2016-01-01C#8.0 中開啟默認(rèn)接口實(shí)現(xiàn)方法
這篇文章主要介紹了C#8.0 中開啟默認(rèn)接口實(shí)現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧的相關(guān)資料2019-05-05C#多線程處理多個隊(duì)列數(shù)據(jù)的方法
本文將結(jié)合實(shí)例代碼,介紹C#多線程處理多個隊(duì)列數(shù)據(jù)的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06