According to the exception information, it indicates that we have no access to Performance Monitor. As WebApp is a sandbox, if we use the Azure WebApp, we have no access to do that.
The user account must either be a member of the Administrators group or a member of the Performance Monitor Users group in Windows.
My suggestion is that we could use Application Insight to do that. We need to configurate Application Insight for WebApp, more details please refer to the document. About Performance Counters in the Application Insight, we could refer to this tutorials.
If we try to use Application Insight API, we need to create a Apikey. We also could get demo code from the document. It works correctly for me.
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;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…