.net 核心中的 CPU 使用率(至少在 Windows 上) [英] CPU usage in .net core (at least on Windows)

查看:19
本文介绍了.net 核心中的 CPU 使用率(至少在 Windows 上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以 PerformanceCounter 在 dotnet 核心中消失了.我知道这是因为它不兼容 Linux.

So PerformanceCounter is gone in dotnet core. I understand it was because it was not Linux-compatible.

这里的评论:(什么是.NET Core 性能计数器的故事? )似乎表明如果我愿意只在 Windows 上运行它,我可以利用 Windows 特定的功能",但我不知道我该怎么做将其集成到 dotnet 核心中.

This comment here: ( What is the story of Performance Counters for .NET Core? ) seems to suggest that if I was willing to run it only on Windows I could "make use of Windows-specific features", but I don't know how I could integrate that in dotnet core.

我愿意采用这种解决方法的原因是保留 dotnet 核心代码库,这样一旦 PerformanceCounter 有跨平台解决方案,我就不必迁移.

The reason I am willing to do this workaround is to keep the dotnet core codebase so I don't have to migrate once there is a cross-platform solution for PerformanceCounter.

总结一下我的问题:在 Windows 上运行时,如何在 dotnet core 中运行 CPU 使用率?

So to summarize my question: how can I run the CPU usage in dotnet core when running on Windows?

我发现的其他相关帖子:

  • 这个解决方案似乎暗示 Process.GetCurrentProcess()够了,我不太明白(PerformanceCounter提供了机器的完整 CPU 使用率).

  • This solution seems to suggest that Process.GetCurrentProcess() would be enough, which I don't quite understand (PerformanceCounterprovided the full CPU usage for the machine).

遍历所有流程(如这篇其他帖子似乎暗示了?) 是不可行的(它会为某些进程抛出异常)并且它看起来也很慢,这对我的用例来说是一个问题.

Looping through all processes (as this other post seem to suggest?) is not feasible (it throws an exception for some processes) and it looks pretty slow as well which is a problem for my use case.

推荐答案

从 .NET Core 3.0 开始,性能计数器 API 再次成为 BCL 的一部分并且也支持 Linux:

As of .NET Core 3.0, the Performance Counter APIs are once again part of the BCL and support Linux too:

MS DevBlog:net-core 3.0 中的诊断改进

所以:Linux 示例

这篇关于.net 核心中的 CPU 使用率(至少在 Windows 上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆