开发者

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.N开发者_JAVA技巧ET, how can I tell when the app domain last recycled?


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) https://web.archive.org/web/20211020113638/https://www.4guysfromrolla.com/articles/041002-1.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜