SIGN UP MEMBER LOGIN:    
ARTICLE

System components performance in ASP.NET using C#

Posted by Sateesh Arveti Articles | Coding Best Practices July 01, 2006
This article shows how to retrieve system properties in a webpage on just singleclick of mouse.
Reader Level:
Download Files:
 

Normally, if we want to know about your system components performance like browser, cache, System the only option is to meet our System administrator. But, this application will help us to know about our system a lot.

This application displays System component's performance in web page. From that, we can get details of our system.

I created this application in VS.NET 2003 using C#.

First, create a Web application. Add 3 web pages to it. Finally, solution explorer will be like this:

components1.gif 

Next go to WebForm1.aspx, write this code in body tag:

onload="loader()"

Next write this JavaScript code as shown in below figure:

components2.gif

function
loader()

{

          handle=window.open("WebForm2.aspx","handle","toolbar=no,location=no,status=no,menubar=no,scrollbars=no,width=   500,height=260,left=510,top=450");

          self.opener=this;

          self.close();

}

This will open the next window(WebFrom2.aspx) in a small size, as shown in below:

components3.gif 

Next go to WebForm2.aspx, place the controls as shown below:

components4.gif 

Next add these items to "Available Categories" combobox:

  • Cache
  • System
  • Server
  • Memory
  • Browser

Next set AutoPostBack to true for both comboboxes.

By this application, we are going to get properties of above objects of our system.

Add this variable 

public string selectedCategory;

Next go to Page_load event in webform2.aspx.cs:

selectedCategory="cache";

Button1.Attributes.Add("OnClick","showwindow()");

if(!IsPostBack)

{

          System.Diagnostics.PerformanceCounter[] pc;

          System.Diagnostics.PerformanceCounterCategory cat =

          new System.Diagnostics.PerformanceCounterCategory("cache");

          DropDownList2.Items.Clear();

          pc = cat.GetCounters();

          for (int i = 0 ; i <pc.Length  ; i++)

          {

                   DropDownList2.Items.Add(pc[i].CounterName);

          }

}

I am using performancecounter to monitor system component's. First, I add JavaScript code to open a new window for showing each component's properties on Button click. First, I displayed properties of cache object, when page loads. Next go to DropDownList1_SelectedIndexChanged event and write this code:

System.Diagnostics.PerformanceCounter[] pc;

System.Diagnostics.PerformanceCounterCategory cat =

new  System.Diagnostics.PerformanceCounterCategory(DropDownList1.SelectedItem.Text.ToString());

DropDownList2.Items.Clear();

pc = cat.GetCounters();

for (int i = 0 ; i <pc.Length  ; i++)

{

          DropDownList2.Items.Add(pc[i].CounterName);

}

selectedCategory=DropDownList1.SelectedItem.Text.ToString();

This will load selected item properties in sub-category combobox. Next go to DropDownList2_SelectedIndexChanged event and write this code:

System.Diagnostics.PerformanceCounter pc=new System.Diagnostics.PerformanceCounter();

pc.CategoryName =DropDownList1.SelectedItem.Text.ToString();

pc.CounterName  =DropDownList2.SelectedItem.Text.ToString();

pc.MachineName =".";

pc.InstanceName =string.Empty;

Label3.Text =DropDownList2.SelectedItem.Text.ToString()+":"+pc.NextValue().ToString();

Next go to WebForm2.aspx and write this JavaScript code as shown below in HEAD tag:

<script language="javascript">
function
showwindow()
{
handle1=window.open("WebForm3.aspx?selectedcategory="+"","handle1","toolbar=no,location=no,status=no,menubar=no,scrollbars=no,width=850,height=600");
}
</script>

Next go to WebFrom3.aspx and add a datagrid and format, set AllowPaging=true and PageSize=8 and go to events of DataGrid and select DataGrid1_PageIndexChanged and write this code:

DataGrid1.CurrentPageIndex  =e.NewPageIndex;

to allow paging for DataGrid.

Next go to Page_Load event of WebForm3.aspx:

string category=Request.QueryString[0];

System.Diagnostics.PerformanceCounter[] pc;

System.Diagnostics.PerformanceCounterCategory cat =

new System.Diagnostics.PerformanceCounterCategory(category);

pc = cat.GetCounters();

StreamWriter writer=File.CreateText("d:\\file2.xml");

writer.WriteLine("<?xml version='1.0'?>");

writer.Write("<"+category+">");

ListItemCollection col=new ListItemCollection();

for (int i = 0 ; i <pc.Length  ; i++)

{

          ListItem item=new ListItem();

          item.Text =pc[i].CounterName.ToString();

          item.Value  =pc[i].CounterName.ToString();

          col.Add(item);

}

for (int i = 0 ; i <pc.Length  ; i++)

{

          writer.Write("<subcategory>");

          if(pc[i].CounterName.ToString().IndexOf("/")!=-1)

          {

                   string countername=pc[i].CounterName.ToString().Replace("/"," per ");

                   writer.Write("<name>"+countername+"</name>");

          }

          else

          {

                    writer.Write("<name>"+pc[i].CounterName.ToString()+"</name>");

          }

          if(pc[i].CounterName == col.FindByText(pc[i].CounterName.ToString()).Text.ToString())

          {

                   System.Diagnostics.PerformanceCounter pc1=new System.Diagnostics.PerformanceCounter();

                   pc1.CategoryName=category;

                   pc1.CounterName=pc[i].CounterName.ToString();

                   pc1.MachineName =".";

                   pc1.InstanceName =string.Empty;

                   writer.Write("<value>"+pc[i].NextValue().ToString()+"</value>");

                   writer.Write("</subcategory>");

          }

}

writer.Write("</"+category+">");

writer.Flush();

writer.Close();

DataSet ds=new DataSet();

ds.ReadXml("d:\\file2.xml");

DataGrid1.DataSource =ds.Tables[0];

DataGrid1.DataBind();

File.Delete("d:\\file2.xml");

This code will display details of selected item(like browser, cache) properties and values in grid.

I am just getting selected item using QueryString and storing its properties and values in a XML file and showing that contents in a grid.

Final, screen will be like this:

components5.gif

By using this code, even a ordinary person can know about his system properties. So, I think no need of System admin from now onwards, if we enhance this application. Just add required objects to Categories combobox and display its properties in Grid. If you have any doubts, just drag PerformanceCounter object from Components tab of toolbox and see CategoryName, CounterName, InstanceName properties....

I hope this code will be useful for all.

Login to add your contents and source code to this article
share this article :
post comment
 

Nice one. Appreciate your effort.

Posted by Krishnaraj L Jul 03, 2006
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor