Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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(): http://blogs.msdn.com/b/bclteam/archive/2006/06/02/618156.aspx

HTH

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...