简体   繁体   English

.NET 4.0和Windows 7上的PerformanceCounters

[英]PerformanceCounters on .NET 4.0 & Windows 7

I have a program that works fine on VS2008 and Vista, but I'm trying it on Windows 7 and VS2010 / .NET Framework 4.0 and it's not working. 我有一个可以在VS2008和Vista上正常运行的程序,但是我正在Windows 7和VS2010 / .NET Framework 4.0上进行尝试,但无法正常工作。 Ultimately the problem is that System.Diagnostics.PerformanceCounterCategory.GetCategories() (and other PerformanceCounterCategory methods) is not working. 最终,问题是System.Diagnostics.PerformanceCounterCategory.GetCategories() (和其他PerformanceCounterCategory方法)无法正常工作。 I'm getting a System.InvalidOperationException with the message "Cannot load Counter Name data because an invalid index '' was read from the registry." 我收到一条System.InvalidOperationException,消息为“由于从注册表中读取了无效的索引”,因此无法加载计数器名称数据。”

I can reproduce this with the very simple program shown below: 我可以使用下面显示的非常简单的程序来重现它:

class Program
{
    static void Main(string[] args)
    {
        foreach (var pc in System.Diagnostics.PerformanceCounterCategory.GetCategories())
        {
            Console.WriteLine(pc.CategoryName);
        }
    }
}

I did make sure I'm running the program as an admin. 我确实确保以管理员身份运行该程序。 It doesn't matter if I run it with VS/Debugger attached or not. 是否连接VS / Debugger都可以运行。 I don't have another machine with Windows 7 or VS2010 to test it on, so I'm not sure which is complicating things here (or both?). 我没有另一台装有Windows 7或VS2010的计算机来对其进行测试,因此我不确定哪一台计算机使这里的事情复杂化(或同时使两者复杂化)。 It is Windows 7 x64 and I've tried forcing the app to run in both x32 and x64 but get the same results. 它是Windows 7 x64,我尝试强制该应用程序同时在x32和x64中运行,但得到的结果相同。

It seems performance counters were corrupted on my system. 看来性能计数器在我的系统上已损坏。 Although I didn't follow this post exactly, it led me to the solution. 尽管我没有完全按照这篇文章发表 ,但它使我找到了解决方案。 Here is what I did: 这是我所做的:

In an command prompt with administrator/elevate privileges typed the following: 在具有管理员/提升权限的命令提示符下,键入以下内容:

lodctr /?

Useful stuff in there... 那里有用的东西...

Then typed: 然后输入:

lodctr /R

According to the docs from the prior step, this gets windows to rebuild the perf registry strings and info from scratch based on the current registry settings and backup INI files. 根据上一步中的文档,这将使Windows 根据当前的注册表设置和备份INI文件从头开始重建perf注册表字符串和信息。 I have a feeling this is what did the magic. 我感觉这就是魔术。 However, next I noticed the .NET performance counters were not there anymore so based on this I typed the following to reload them: 但是,接下来我注意到.NET性能计数器不再存在,因此基于此,我键入以下内容以重新加载它们:

lodctr "C:\Windows\Microsoft.NET\Framework64\v4.0.20506\corperfmonsymbols.ini"

Note that this path is for .NET Framework 4.0 on x64. 请注意,此路径适用于x64上的.NET Framework 4.0。 You can imagine the path for other variations of the framework/platform. 您可以想象框架/平台的其他变体的路径。 I'm guessing you should always load the counters from the highest version of the .NET framework that you have installed, but that is just a guess. 猜测您应该始终从已安装的最高版本的.NET框架加载计数器,但这只是一个猜测。

I hope this helps someone else someday! 希望有一天能对其他人有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM