Programmatically find when the ASP.NET worker process and app domain last started?

In ASP.NET:

  1. How can I tell when the ASP.NET worker process last restarted?
  2. In ASP.NET, how can I tell when the app domain last recycled?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

For the worker process, you can programmatically read Process -> Elapsed Time from the corresponding perf. counter (1) or directly from the System.Diagnostics.Process namespace; for the AppDomain, you can set an application-level variable at start-up to serve as your baseline and measure against that manually.

Scott Mitchell actually has a couple of good posts on this that are still relevant 8 years later, believe it or not (2). Running on Cassini (Vista, VS 2008), I’m seeing an accurate system up-time with:

TimeSpan.FromMilliseconds(Environment.TickCount)

…and accurate Process/AppDomain up-times with these:

foreach (Process p in Process.GetProcessesByName("WebDev.WebServer"))
{
    Response.Write(DateTime.Now.Subtract(p.StartTime).ToString() + "<br/>");
}

Response.Write(DateTime.Now.Subtract((DateTime)Application["StartTime"]).ToString());

I’m also able to obtain the correct PerfomanceCounter, but can’t seem to read the correct value (always zero, under my setup):

Response.Write(new PerformanceCounter("Process", "Elapsed Time", "WebDev.WebServer").NextValue() + "<br/>");

Scott’s articles are definitely worth a read – there’s a wealth of info in ProcessInfo and ProcessModelInfo (eg. ProcessModelInfo.GetHistory), but unfortunately it’s not available on Cassini:

Process metrics are available only
when the ASP.NET process model is
enabled. When running on versions of
IIS 6 or newer in worker process
isolation mode, this feature is not
supported.

UPDATE:
great explanation of how to read the counter correctly to avoid the zero on NextValue(): Link

HTH

(1) https://serverfault.com/questions/90927/lifetime-of-iis-worker-process-or-appdomai

(2) http://www.4guysfromrolla.com/articles/041002-1.aspx


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x