性能计数器抛出SecurityException [英] Performance counter throws SecurityException

查看:99
本文介绍了性能计数器抛出SecurityException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

    private static void CreateCounter()
    {
        if (PerformanceCounterCategory.Exists("DemoCategory"))
            PerformanceCounterCategory.Delete("DemoCategory");

        CounterCreationDataCollection ccdArray = new CounterCreationDataCollection();

        CounterCreationData ccd = new CounterCreationData();
        ccd.CounterName = "RequestsPerSecond";
        ccd.CounterType = PerformanceCounterType.NumberOfItems32;
        ccd.CounterHelp = "Requests per second";
        ccdArray.Add(ccd);

        PerformanceCounterCategory.Create("DemoCategory", "Demo category",
            PerformanceCounterCategoryType.SingleInstance, ccdArray);

        Console.WriteLine("Press any key, to start use the counter");
    }

很明显:

PerformanceCounterCategory.Create("DemoCategory", "Demo category", 
     PerformanceCounterCategoryType.SingleInstance, ccdArray);

是引发异常的行.

我已经读过有关PerformanceCounterPermission的信息,我该怎么办?

I have read about PerformanceCounterPermission, what should I do exactly?

推荐答案

您的应用程序进程没有适当的特权级别.这就是安全异常告诉您的内容.

Your application's process does not have the appropriate privilege level. That's what the security exception is telling you.

简单的解决方法是在应用程序启动时请求该权限.您可以通过修改应用程序的清单以将requestedExecutionLevel设置为requireAdministrator来实现.

The simple fix is to request that permission when your application starts. You can do this by modifying your application's manifest such that the requestedExecutionLevel is set to requireAdministrator.

添加到应用程序清单中的完整部分将如下所示:

The complete section added to your application's manifest will look something like this:

<!-- Identify the application security requirements. -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
  <security>
    <requestedPrivileges>
      <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
    </requestedPrivileges>
  </security>
</trustInfo>

如果您的应用程序否则不需要管理特权,则可能会有更好的选择,因为您应始终以绝对必要或必需的最低特权级别运行.您可以使用Google调查这些替代方案;它将涉及分离一个单独进程,该进程请求UAC提升并在用户明确请求时运行性能计数器.

There are potentially better alternatives if your application does not otherwise require administrative privileges, because you should always run at the lowest privilege level that is absolutely necessary or required. You can investigate these alternatives using Google; it's going to involve spinning off a separate process that requests UAC elevation and runs the performance counter when requested explicitly by the user.

这篇关于性能计数器抛出SecurityException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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