欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.NET使用IResourceMonitor實(shí)現(xiàn)獲取資源信息

 更新時間:2024年01月22日 14:30:18   作者:rjcql  
在 Microsoft.Extensions.Diagnostics.ResourceMonitoring 包提供了一系列定制 API,專用于監(jiān)視 .NET 應(yīng)用程序的資源利用率,本文將利用IResourceMonitor來實(shí)現(xiàn)獲取資源狀態(tài)信息,感興趣的可以了解下

寫在前面

 在 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)文章

最新評論