Azure - 如何读取我的 Web 应用程序的 cpu 和内存?
Azure - How can I read cpu and memory of my web app?
我正在尝试使用 PerformanceCounters
读取我的应用程序的 CPU 和内存使用情况。
代码:
PerformanceCounter cpuCounter;
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
var result = cpuCounter.NextValue();//ERROR HERE
我遇到了未经授权的异常。
我该如何解决这个问题?
编辑 1:
我尝试为处理器数量和内存设置当前实例名称,但运气不佳...
编辑 2:
例外 .ToString()
是
System.UnauthorizedAccessException: Access to the registry key 'Global' is denied. at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str) at Microsoft.Win32.RegistryKey.InternalGetValue(String name, Object defaultValue, Boolean doNotExpand, Boolean checkSecurity) at Microsoft.Win32.RegistryKey.GetValue(String name) at System.Diagnostics.PerformanceMonitor.GetData(String item) at System.Diagnostics.PerformanceCounterLib.GetPerformanceData(String item) at System.Diagnostics.PerformanceCounterLib.get_CategoryTable() at System.Diagnostics.PerformanceCounterLib.CounterExists(String category, String counter, Boolean& categoryExists) at System.Diagnostics.PerformanceCounterLib.CounterExists(String machine, String category, String counter) at System.Diagnostics.PerformanceCounter.InitializeImpl() at System.Diagnostics.PerformanceCounter.Initialize() at System.Diagnostics.PerformanceCounter.NextSample() at System.Diagnostics.PerformanceCounter.NextValue() at StudioTech.Web.Infrastructure.CustomMachineMonitoring.<>c__DisplayClass0_0.<b__0>d.MoveNext() in C:\MMT\One\StudioTech.Web\Infrastructure\CustomMachineMonitoring.cs:line 33
根据异常信息,说明我们没有权限访问Performance Monitor。由于 WebApp 是 sandbox,如果我们使用 Azure WebApp,我们无权这样做。
The user account must either be a member of the Administrators group or a member of the Performance Monitor Users group in Windows.
我的建议是我们可以使用 Application Insight 来做到这一点。我们需要为WebApp配置Application Insight,更多细节请参考document. About Performance Counters in the Application Insight, we could refer to this tutorials.
如果我们尝试使用 Application Insight API,我们需要 create a Apikey. We also could get demo code from the document。它对我来说工作正常。
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;
}
}
我正在尝试使用 PerformanceCounters
读取我的应用程序的 CPU 和内存使用情况。
代码:
PerformanceCounter cpuCounter;
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
var result = cpuCounter.NextValue();//ERROR HERE
我遇到了未经授权的异常。 我该如何解决这个问题?
编辑 1:
我尝试为处理器数量和内存设置当前实例名称,但运气不佳...
编辑 2:
例外 .ToString()
是
System.UnauthorizedAccessException: Access to the registry key 'Global' is denied. at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str) at Microsoft.Win32.RegistryKey.InternalGetValue(String name, Object defaultValue, Boolean doNotExpand, Boolean checkSecurity) at Microsoft.Win32.RegistryKey.GetValue(String name) at System.Diagnostics.PerformanceMonitor.GetData(String item) at System.Diagnostics.PerformanceCounterLib.GetPerformanceData(String item) at System.Diagnostics.PerformanceCounterLib.get_CategoryTable() at System.Diagnostics.PerformanceCounterLib.CounterExists(String category, String counter, Boolean& categoryExists) at System.Diagnostics.PerformanceCounterLib.CounterExists(String machine, String category, String counter) at System.Diagnostics.PerformanceCounter.InitializeImpl() at System.Diagnostics.PerformanceCounter.Initialize() at System.Diagnostics.PerformanceCounter.NextSample() at System.Diagnostics.PerformanceCounter.NextValue() at StudioTech.Web.Infrastructure.CustomMachineMonitoring.<>c__DisplayClass0_0.<b__0>d.MoveNext() in C:\MMT\One\StudioTech.Web\Infrastructure\CustomMachineMonitoring.cs:line 33
根据异常信息,说明我们没有权限访问Performance Monitor。由于 WebApp 是 sandbox,如果我们使用 Azure WebApp,我们无权这样做。
The user account must either be a member of the Administrators group or a member of the Performance Monitor Users group in Windows.
我的建议是我们可以使用 Application Insight 来做到这一点。我们需要为WebApp配置Application Insight,更多细节请参考document. About Performance Counters in the Application Insight, we could refer to this tutorials.
如果我们尝试使用 Application Insight API,我们需要 create a Apikey. We also could get demo code from the document。它对我来说工作正常。
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;
}
}