Is there a way to show CPU and RAM usage statistics on an asp.net page. I've tried this code but I have error:
有没有办法在asp.net页面上显示CPU和RAM使用情况统计信息。我试过这段代码,但是我有错误:
Access to the registry key 'Global' is denied.
on this line:
在这条线上:
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
4 个解决方案
#1
5
As mentioned in comments, without the appropriate permissions you will not be able to do this. Resource statistics will include a lot of information about processes owned by other users of the system which is privileged information
如评论中所述,如果没有相应的权限,您将无法执行此操作。资源统计信息将包含许多有关系统其他用户拥有的进程的信息,这些信息是特权信息
#2
5
Use:
System.Diagnostics.PerformanceCounter cpuUsage =
new System.Diagnostics.PerformanceCounter();
cpuUsage.CategoryName = "Processor";
cpuUsage.CounterName = "% Processor Time";
cpuUsage.InstanceName = "_Total";
float f = cpuUsage.NextValue();
Edit:
Windows limits access to the performance counters to those in the Administrators or Performance Logs Users (in Vista+) groups. Setting registry security won't resolve this. It is possible that there is a user right attached to it somewhere.
Windows将性能计数器的访问权限限制为管理员或性能日志用户(在Vista +中)组中的访问权限。设置注册表安全性不会解决此问题。有可能在某处附加了用户权限。
#3
0
to do not call .NextValue() twice you can use MVC "global variables:
不要调用.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;
}
}
}
public class JsonContent : HttpContent { private readonly JToken _value;
public class 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;
}
}
#4
-3
Use:
new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
Instead of:
new PerformanceCounter("Processor", "% Processor Time", "_Total");
#1
5
As mentioned in comments, without the appropriate permissions you will not be able to do this. Resource statistics will include a lot of information about processes owned by other users of the system which is privileged information
如评论中所述,如果没有相应的权限,您将无法执行此操作。资源统计信息将包含许多有关系统其他用户拥有的进程的信息,这些信息是特权信息
#2
5
Use:
System.Diagnostics.PerformanceCounter cpuUsage =
new System.Diagnostics.PerformanceCounter();
cpuUsage.CategoryName = "Processor";
cpuUsage.CounterName = "% Processor Time";
cpuUsage.InstanceName = "_Total";
float f = cpuUsage.NextValue();
Edit:
Windows limits access to the performance counters to those in the Administrators or Performance Logs Users (in Vista+) groups. Setting registry security won't resolve this. It is possible that there is a user right attached to it somewhere.
Windows将性能计数器的访问权限限制为管理员或性能日志用户(在Vista +中)组中的访问权限。设置注册表安全性不会解决此问题。有可能在某处附加了用户权限。
#3
0
to do not call .NextValue() twice you can use MVC "global variables:
不要调用.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;
}
}
}
public class JsonContent : HttpContent { private readonly JToken _value;
public class 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;
}
}
#4
-3
Use:
new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
Instead of:
new PerformanceCounter("Processor", "% Processor Time", "_Total");