November 27, 2014
Hot Topics:

ASP.NET Secrets, Part 5

  • April 21, 2003
  • By Karl Moore
  • Send Email »
  • More Articles »

Spy on Your Web Host: Checking Uptime in .NET

Okay, so your host promises 99.999% uptime, but are they really delivering? Unless you're either checking every couple of seconds, or dishing out on some monitoring service, you really don't have much of an idea.

That is, until now.

ASP.NET Web pages served up through IIS are delivered through an ASP.NET "worker process" (aspnet_wp.exe). It's this file that executes your code and puts all the ASP.NET pieces together. If we could access this worker process, we may be able to look at how long it had been running, its current state—and perhaps even a history, showing previous instantiations of the worker process.

Well, you can. And the following snippet of code shows you exactly how:

Dim strReport As String
Dim objInfo As ProcessInfo = _
            ProcessModelInfo.GetCurrentProcessInfo
' Get time information
strReport = "ASP.NET was started at " & _
    objInfo.StartTime.ToString & ". " & _
    "It has been running for " & _
    objInfo.Age.Days & " days, " & _
    objInfo.Age.Hours & " hours and " & _
    objInfo.Age.Minutes & " minutes. "
Response.Write(strReport)
' Get other info
strReport = "The process ID is " & _
    objInfo.ProcessID & ". " & _
    "Current status is " & _
    objInfo.Status.ToString & ". " & _
    "Peak memory used was " & _
    objInfo.PeakMemoryUsed & ". " & _
    "Request count is currently " & _
    objInfo.RequestCount & "."
Response.Write(strReport)

It may look a little weird, but all we're doing here is retrieving a ProcessInfo object containing information about the current worker process. This is then formulated into a string using key properties and spurted out to the user, through Response.Write. Try it out—you'll be given an instant server status report.

The ProcessModelInfo class also has a GetHistory method, allowing you to retrieve information about previous instances of the aspnet_wp.exe process. It returns an array of ProcessInfo objects. Either use a For...Next loop to retrieve this information, or bind direct to a DataGrid.

There's also a technique for retrieving the amount of time your actual server has been running (not just the ASP.NET worker process). Simply nab the TickCount from the Environment class, which lists the number of milliseconds the machine has been running. After that, either perform a few simple calculations—or, slightly easier, convert it into a period of time (a TimeSpan object), then retrieve the appropriate properties. Here's my snippet of code that does exactly that:

Dim tsAge As TimeSpan = _
   TimeSpan.FromMilliseconds(Environment.TickCount)
Dim intDaysUp As Integer = tsAge.Days
Dim intHoursUp As Integer = tsAge.Hours
Dim intMinsUp As Integer = tsAge.Minutes
Response.Write("The server has been up " & _
   intDaysUp & " days, " & intHoursUp & _
   " hours and " & intMinsUp & " minutes.")

Caption: ASP.NET only been running for 13 minutes? Sounds dodgy. Change your host.





Page 3 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel