我尝试为我的 MVC 操作创建性能计数器,但收到一个 SecurityException,指出注册表访问被拒绝。我已向 web.config 文件添加了完全信任,但这并不能解决问题。
PerformanceCounterCategory performancecountercategory = null;
if (!PerformanceCounterCategory.Exists(categoryname))
{
performancecountercategory = PerformanceCounterCategory.Create(categoryname, "Latency counters for the Media Gateway", PerformanceCounterCategoryType.SingleInstance, countercreationdata);
}
else
{
performancecountercategory = new PerformanceCounterCategory(Constants.PerformanceCounterCategory);
}
<system.web>
<trust level="Full"></trust>
<compilation targetFramework="4.5" debug="true" />
<httpRuntime targetFramework="4.5" />
最佳答案
这不是中等信任/完全信任问题。您需要以管理员或其他特权用户身份运行才能在计算机上创建性能计数器。见http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecountercategory(v=vs.110).aspx备注部分:
It is strongly recommended that new performance counter categories be created during the installation of the application, not during the execution of the application.
基本上,如果您的应用程序有安装程序,您将在安装时调用 PerformanceCounterCategory.Create。如果您的应用程序没有安装程序,您可以使用此代码编写 .exe 并从提升的命令提示符运行它。
然后,作为 MVC 应用程序正常执行的一部分,您可以利用该性能计数器已经存在的事实,通过使用标准 PerformanceCounter 类打开计数器并向其中写入值。
关于c# - ASP.NET MVC : Requested registry access denied when creating performance counters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22616361/