Windows performance monitoring in ASP.NET(C#)?

Using the namespace of System.Diagnostics to monitor Windows performance, e.g. performance counter,threads and processes.
‘To get the data from Windows performance counter

using System;
using System.Net;
using System.Diagnostics;

At first declare three PerformanceCounter instances, define the category and counters in performance monitor
Then display the values obtained in different Labels.

By the looping of PerformanceCounterCategory.GetCategories method to go through all available categories

PerformanceCounter objMemperf = new PerformanceCounter(”Memory”,”Available Bytes”);
PerformanceCounter objProcperf = new PerformanceCounter(”System”, “Processes”);
PerformanceCounter objComperf = new PerformanceCounter(”System”, “Threads”);
Label1.Text = string.Format(”{0:#,###}”, objMemperf.NextValue()) + “Byte”;
Label2.Text = objProcperf.NextValue().ToString();
Label3.Text = objComperf.NextValue().ToString();
if (!Page.IsPostBack)
{
foreach(PerformanceCounterCategory objPer in PerformanceCounterCategory.GetCategories())
{
ListBox1.Items.Add(new ListItem(objPer.CategoryName));
}
}
The whole code behind front page
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Diagnostics;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PerformanceCounter objMemperf = new PerformanceCounter(”Memory”,”Available Bytes”);
PerformanceCounter objProcperf = new PerformanceCounter(”System”, “Processes”);
PerformanceCounter objComperf = new PerformanceCounter(”System”, “Threads”);

Label1.Text = string.Format(”{0:#,###}”, objMemperf.NextValue()) + “Byte”;
Label2.Text = objProcperf.NextValue().ToString();
Label3.Text = objComperf.NextValue().ToString();

if (!Page.IsPostBack)
{
foreach(PerformanceCounterCategory objPer in PerformanceCounterCategory.GetCategories())
{
ListBox1.Items.Add(new ListItem(objPer.CategoryName));
}
}
}
}

Posted by Mahesh ( Tryangled )

Leave a Reply

You must be logged in to post a comment.