在 C# 中获取应用程序池正常运行时间
有没有办法可以确定应用程序池(在 IIS7 中)在 C# 中已经运行了多长时间(自启动以来的时间或上次重新启动的时间)?
Is there a way I can determine how long an application pool (in IIS7) has been up (time since started, or last restart) in c#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Process.GetCurrentProcessInfo()
不存在。Process.GetCurrentProcessInfo()
doesn't exist.非常愚蠢的技巧:在所有东西都使用的某个类中,使用类构造函数来记住您的开始时间并使用 aspx 页面来接收它。现在与当前时间进行比较。
Really stupid trick: in some class that everything uses, use a class constructor to remember your start time and use an aspx page to receive it. Now compare to current time.
在 ASP.NET 应用程序中,您可以尝试
TimeSpan uptime = (DateTime.Now - ProcessInfo.GetCurrentProcessInfo ().StartTime)
From the ASP.NET application, you can try
TimeSpan uptime = (DateTime.Now - ProcessInfo.GetCurrentProcessInfo ().StartTime)
基于上面的内容,我创建了一个像这样的简单类..
并在 Global.asax.cs 中的 Application_Start() 中调用它,
然后可以使用它在任何地方访问
Based on the above I created a simple class like so..
and called it in Application_Start() in Global.asax.cs like
It can then be accessed anywhere using
如果您发现 Process.GetCurrentProcessInfo() 不存在,正如其他用户提到的那样,
可能适合您。
(我想将此添加为 Eric Humphrey 帖子的评论,但不允许)
if you find that Process.GetCurrentProcessInfo() doesn't exist as another user mentioned,
may work for you.
(I wanted to add this as a comment to Eric Humphrey's post but I'm not allowed)
我个人使用两种方法之一。使用静态类(如@Original10的答案所示)或使用
Application
变量。我发现使用
Application
变量是可以接受的,因为我注意到Process.GetCurrentProcess()
在应用程序重新启动后(例如修改 web.config 或 bin 目录)仍然存在。我还需要一些能够满足网站重新启动的需求。。在 Global.asax 中,将以下内容添加到
Application_Start
- 并添加该方法(如果不存在)。在您需要的代码中,您可以执行以下操作:
这将产生类似于以下内容的内容
:如果需要,不会检查应用程序变量或锁定应用程序。我将把这个作为练习留给用户。
One of two approaches exist that I personally use. Using a static class (as shown in @Original10's answer) or using
Application
variables.I have found that using
Application
variables is acceptable because I noticedProcess.GetCurrentProcess()
survives application restarts (eg modification of web.config or bin directory). I needed something that would cater for the website restarting as well.In your Global.asax, add the following to the
Application_Start
- and add the method it if it's not there.In your code where you need it, you could do something like:
Which will produce something along the lines of
There is no checking of the application variable or locking of the application if required. I'll leave this as an exercise to the user.
如果您将重新启动(回收)应用程序池与http://forums.iis.net/t/1162615.aspx,你应该得到它
If you mashed Restarting (Recycling) an Application Pool and http://forums.iis.net/t/1162615.aspx, you should get it