

Navigator : Home > Tutorials > Performance Tutorials > ...
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; |
At first declare three PerformanceCounter instances, define the category and counters in performance monitor
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
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)); } } |
The front page of Default.aspx
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
<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> |
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!
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.