ASP.NET:性能计数器不显示任何内容

发布于 2024-12-14 15:29:50 字数 1320 浏览 2 评论 0原文

我是 asp.net、C# 等方面的完全初学者...

我看到 这个这个

然后我尝试这个:

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Text;
using System.Net;
using System.Collections.Generic;
using System.Xml;

using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Response.Write("Hello from code behind");

        PerformanceCounter cpuload = new PerformanceCounter();
        cpuload.CategoryName = "Processor";
        cpuload.CounterName = "% Processor Time";
        cpuload.InstanceName = "_Total";

        //or PerformanceCounter cpuload = new PerformanceCounter("Processor", "% Processor Time", "_Total");

        Console.WriteLine(cpuload.NextValue() + "%");

    }
}

但这只显示 Hello from code Behind< /代码>。为什么请?

I am a complete beginner in asp.net, C#, etc...

I saw this and this

Then I try this:

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Text;
using System.Net;
using System.Collections.Generic;
using System.Xml;

using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Response.Write("Hello from code behind");

        PerformanceCounter cpuload = new PerformanceCounter();
        cpuload.CategoryName = "Processor";
        cpuload.CounterName = "% Processor Time";
        cpuload.InstanceName = "_Total";

        //or PerformanceCounter cpuload = new PerformanceCounter("Processor", "% Processor Time", "_Total");

        Console.WriteLine(cpuload.NextValue() + "%");

    }
}

But that only displays Hello from code behind. Why please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟到的我 2024-12-21 15:29:50

您正在写入控制台,这是行不通的,请改为写入响应(就像您的字符串一样):

Response.Write(cpuload.NextValue().ToString() + "%");

虽然严格来说不是控制台应用程序的应用程序类型可以具有关联的控制台,但除非明确指定,否则它们是不可见的这样做了,并且不能在网络应用程序中。

You're writing to the console, which won't work, write to the response instead (just as your string does):

Response.Write(cpuload.NextValue().ToString() + "%");

While applications types that aren't strictly console applications can have an associated console, they're not visible unless explicitly made so, and couldn't be in a web application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文