CPU使用率有沒有辦法顯示一個asp.net頁面上的CPU和內存使用情況的統計數據。我試過this代碼,但我有錯誤:如何獲得在asp.net
Access to the registry key 'Global' is denied.
在這條線:正如在評論中提到
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
CPU使用率有沒有辦法顯示一個asp.net頁面上的CPU和內存使用情況的統計數據。我試過this代碼,但我有錯誤:如何獲得在asp.net
Access to the registry key 'Global' is denied.
在這條線:正如在評論中提到
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
,沒有相應的權限,您將無法做到這一點。資源統計將包括有關係統是特權信息
我相信你是正確的...但你能提供可能的詳細信息...假設你遇到了這個自己,知道需要什麼樣的權限,並沒有什麼.. – Seabizkit 2015-10-19 15:29:11
使用的其他用戶擁有的進程很多信息:
System.Diagnostics.PerformanceCounter cpuUsage =
new System.Diagnostics.PerformanceCounter();
cpuUsage.CategoryName = "Processor";
cpuUsage.CounterName = "% Processor Time";
cpuUsage.InstanceName = "_Total";
float f = cpuUsage.NextValue();
編輯:
的Windows限制訪問性能計數器那些管理員或性能日誌用戶(在Vista +中)組。設置註冊表安全無法解決此問題。有可能在某處附加了用戶權限。
我得到0(零).. – 2012-06-22 16:09:16
您需要運行'cupUsage.NextValue()'兩次...第一次總是結果爲0,第二次調用將返回自上次調用以來的平均CPU使用率。 – saluce 2014-04-29 20:48:18
用途:
new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
相反的:
new PerformanceCounter("Processor", "% Processor Time", "_Total");
OP爲什麼要這樣做?請解釋! – markus 2012-12-11 23:06:39
這並沒有真正的幫助。但是,這樣做的功能是在主板上有多個物理處理器(CPU)的情況下提供信息,該處理器具有多個可能是超線程的核心。實例名稱以「0,0」表示第一個CPU上的第一個內核,或者「1,_Total」表示第二個CPU的平均值,以及整個CPU集羣的「_Total」。它提供了關於物理處理能力的更多信息,但它不能提供任何幫助原始問題的信息。 – saluce 2014-04-29 19:16:17
如何解決這些問題**「訪問註冊表項'全局'被拒絕。」**? – Seabizkit 2015-10-19 17:07:07
到不叫.NextValue()兩次,您可以使用MVC「全局變量:
[AllowAnonymous]
[HttpGet]
[ActionName("serverusage")]
public HttpResponseMessage serverusage()
{
try
{
PerformanceCounter cpuCounter;
if (HttpContext.Current.Application["cpuobj"] == null)
{
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
HttpContext.Current.Application["cpuobj"] = cpuCounter;
}
else
{
cpuCounter = HttpContext.Current.Application["cpuobj"] as PerformanceCounter;
}
JToken json;
try
{
json = JObject.Parse("{ 'usage' : '" + HttpContext.Current.Application["cpu"] + "'}");
}
catch
{
json = JObject.Parse("{ 'usage' : '" + 0 + "'}");
}
HttpContext.Current.Application["cpu"] = cpuCounter.NextValue();
var response = Request.CreateResponse(System.Net.HttpStatusCode.OK);
response.Content = new JsonContent(json);
return response;
}
catch (Exception e)
{
var response = Request.CreateResponse(System.Net.HttpStatusCode.BadRequest);
JToken json = JObject.Parse("{ 'problem' : '" + e.Message + "'}");
response.Content = new JsonContent(json);
return response;
}
}
}
公共類JsonContent :HttpContent { private readonly JToken _value;
public JsonContent(JToken value)
{
_value = value;
Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
protected override Task SerializeToStreamAsync(Stream stream,
TransportContext context)
{
var jw = new JsonTextWriter(new StreamWriter(stream))
{
Formatting = Formatting.Indented
};
_value.WriteTo(jw);
jw.Flush();
return Task.FromResult<object>(null);
}
protected override bool TryComputeLength(out long length)
{
length = -1;
return false;
}
}
你試過Process類?即。 'Process.GetCurrentProcess()'和'Process.TotalProcessorTime'?通過測量固定時間內該值的增長情況,您可以輕鬆計算出在此期間您獲得/使用的CPU數量。我留下這個評論,因爲我不知道你是否被允許檢查你發佈的錯誤消息。 – 2011-03-18 19:08:21
你有權訪問IIS管理嗎?如果你這樣做,那麼你可以授予你的工作者訪問這部分註冊表的權限。如果不是 - 例如如果你在共享主機 - 然後抱歉,但你根本沒有訪問權限。 – Stuart 2011-03-18 19:14:42
@Stuard:我有自己的VDS運行windows server 2008.可能最好的解決方案是爲此寫一個windows服務。 – 2011-03-18 20:14:15