问题
In ASP.NET:
How can I tell when the ASP.NET worker process last restarted?
In ASP.NET, how can I tell when the app domain last recycled?
回答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(): 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
来源:https://stackoverflow.com/questions/4056635/programmatically-find-when-the-asp-net-worker-process-and-app-domain-last-starte