Tuesday, 26 July 2011

Windows performance monitoring in ASP.NET(VB)


This tutorial will show you how to use ASP.NET and VB 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...

Imports System
Imports System.Net
Imports System.Diagnostics

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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

Dim objMemperf As New PerformanceCounter("Memory", "Available Bytes")
Dim objProcperf As New PerformanceCounter("System", "Processes")
Dim objComperf As 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 Then
For Each objPer In PerformanceCounterCategory.GetCategories
ListBox1.Items.Add(New ListItem(objPer.CategoryName))
Next
End If
End Sub

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

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>

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!

The whole code behind front page:

Imports System.Diagnostics
Imports System.Web

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim objMemperf As New PerformanceCounter("Memory", "Available Bytes")
Dim objProcperf As New PerformanceCounter("System", "Processes")
Dim objComperf As New PerformanceCounter("System", "Threads")
Dim objPer As New PerformanceCounterCategory
Label1.Text = String.Format("{0:#,###}", objMemperf.NextValue()) & "Byte"
Label2.Text = objProcperf.NextValue().ToString()
Label3.Text = objComperf.NextValue().ToString()

If Page.IsPostBack = False Then
For Each objPer In PerformanceCounterCategory.GetCategories
ListBox1.Items.Add(New ListItem(objPer.CategoryName))
Next
End If
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment