Tuesday, 26 July 2011

Windows performance monitoring in ASP.NET(C#)


This tutorial will show you how to use ASP.NET and C# to monitor Windows performance, e.g. performance counter, threads and processes.

Using the namespace of System.Diagnostics to get the data from Windows performance counter...

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

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

At first declare three PerformanceCounter instances, define the category and counters in performance monitor

Then display the values obtained in different Labels. Label1 is for displaying the available memory, Lable2 is for the current processes numbers, while Lable3 is used for the total processes

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));
}
}

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

The front page of Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Using Performance Counters</title>
</head>
<body>
<form id="form1" runat="server">
<h1>Web Server Stats</h1>
<i>These stats were taken as of <%=DateTime.Now.ToLongDateString() %> at <%=DateTime.Now.ToLongTimeString()%>....</i>
<br />
<br />
<b>Available:</b><asp:Label ID="Label1" runat="server" Width="251px"></asp:Label>
<br />
<b>
<br />
Tatol Processes:</b>
<asp:Label ID="Label2" runat="server" Width="247px"></asp:Label>
<br />
<br />
<b>Total Threding:</b><asp:Label ID="Label3" runat="server" Width="254px"></asp:Label>
      <br />
<br />
<br />
<asp:ListBox ID="ListBox1" runat="server" Height="38px" Width="368px"></asp:ListBox><br />
<br />
<br />
<br />
</form>
</body>
</html>

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

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));
}
}
}

protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{

}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment