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

别来无恙 提交于 2019-12-18 08:59:04

问题


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?


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!