C# Azure-如何读取web应用程序的cpu和内存?
我正在尝试使用C# Azure-如何读取web应用程序的cpu和内存?,c#,azure,azure-web-app-service,azure-performancecounters,C#,Azure,Azure Web App Service,Azure Performancecounters,我正在尝试使用PerformanceCounters读取我的应用程序的CPU和内存使用情况。 代码: 我得到一个未经授权的例外。 我怎样才能解决这个问题 编辑1: 我试图为处理器计数和内存设置当前实例名,但运气不好 编辑2: 异常.ToString()为 System.UnauthorizedAccessException:对注册表项“Global”的访问被拒绝。位于Microsoft.Win32.RegistryKey.Win32的Microsoft.Win32.RegistryKey.Int
PerformanceCounters
读取我的应用程序的CPU和内存使用情况。
代码:
我得到一个未经授权的例外。
我怎样才能解决这个问题
编辑1:我试图为处理器计数和内存设置当前实例名,但运气不好 编辑2:
异常
.ToString()
为
System.UnauthorizedAccessException:对注册表项“Global”的访问被拒绝。位于Microsoft.Win32.RegistryKey.Win32的Microsoft.Win32.RegistryKey.InternalGetValue(字符串名称、对象defaultValue、布尔doNotExpand、布尔checkSecurity)的Microsoft.Win32.RegistryKey.GetValue(字符串名称)的System.Diagnostics.PerformanceMonitor.GetData(字符串项)的Microsoft.Win32.RegistryKey.Win32 errorCode,String str在System.Diagnostics.PerformanceCounterLib.GetPerformanceData(字符串项)在System.Diagnostics.PerformanceCounterLib.get_CategoryTable()在System.Diagnostics.PerformanceCounterLib.CounterExists(字符串类别、字符串计数器、布尔值和类别存在)在System.Diagnostics.PerformanceCounterLib.CounterExists(字符串机器、字符串类别、字符串计数器)在System.Diagnostics.PerformanceCounter.InitializeImpl()在System.Diagnostics.PerformanceCounter.Initialize()在System.Diagnostics.PerformanceCounter.NextValue()在System.Diagnostics.PerformanceCounter.NextValue()中在StudioTech.Web.Infrastructure.CustomMachineMonitoring.c\uuuu显示c:\MMT\One\StudioTech.Web\Infrastructure\CustomMachineMonitoring.cs中的class0\u 0.d.MoveNext():第33行
根据异常信息,这表明我们没有访问性能监视器的权限。由于WebApp是一个应用程序,如果我们使用Azure WebApp,我们没有访问权限 用户帐户必须是Windows中管理员组的成员或性能监视器用户组的成员 我的建议是,我们可以使用Application Insight来实现这一点。我们需要为WebApp配置Application Insight,更多详细信息请参阅。关于Application Insight中的性能计数器,我们可以参考此 如果我们尝试使用Application Insight API,我们需要。我们还可以从中获得演示代码。它对我来说是正确的
static void Main(string[] args)
{
var applicationId = "xxxxxxxx";
var applicationKey = "xxxxxxxx";
var queryPath = "performanceCounters/processCpuPercentage";
var queryType = "metrics";
var str = GetTelemetry(applicationId, applicationKey, queryType, queryPath, "");
}
public static string GetTelemetry(string appid, string apikey,
string queryType, string queryPath, string parameterString)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("x-api-key", apikey);
var req = string.Format(Url, appid, queryType, queryPath, parameterString);
HttpResponseMessage response = client.GetAsync(req).Result;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsStringAsync().Result;
}
else
{
return response.ReasonPhrase;
}
}
请将异常的完整
.ToString()
复制到您的问题中作为编辑。这不是异常的完整.ToString()
。@ScottChamberlain我的错!检查更新!应用程序洞察的问题是其收集频率太慢(每分钟一次)为了诊断我遇到的问题…我希望每分钟至少捕获20次该信息…理想情况下,我每秒钟或更少需要一次…是的,你是对的。应用程序洞察是它的收集频率可能是每分钟一次。但似乎我们现在没有其他方法来获取WebApp的cpu和内存使用率。如果我们若要使用自定义方式获取度量,我们可以使用Azure VM来托管应用程序。
static void Main(string[] args)
{
var applicationId = "xxxxxxxx";
var applicationKey = "xxxxxxxx";
var queryPath = "performanceCounters/processCpuPercentage";
var queryType = "metrics";
var str = GetTelemetry(applicationId, applicationKey, queryType, queryPath, "");
}
public static string GetTelemetry(string appid, string apikey,
string queryType, string queryPath, string parameterString)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("x-api-key", apikey);
var req = string.Format(Url, appid, queryType, queryPath, parameterString);
HttpResponseMessage response = client.GetAsync(req).Result;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsStringAsync().Result;
}
else
{
return response.ReasonPhrase;
}
}