.net核心中的CPU使用率(至少在Windows上) [英] CPU usage in .net core (at least on 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?
我发现的其他相关帖子:
-
此解决方案似乎暗示t
Process.GetCurrentProcess()
就足够了,我不太理解(PerformanceCounter
提供了完整的CPU使用率
This solution seems to suggest that
Process.GetCurrentProcess()
would be enough, which I don't quite understand (PerformanceCounter
provided 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开始,Performance Counter API曾经是再次成为BCL的一部分,并且也支持Linux:
As of .NET Core 3.0, the Performance Counter APIs are once again part of the BCL and support Linux too:
这篇关于.net核心中的CPU使用率(至少在Windows上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!