在 C# 中获取应用程序池正常运行时间

发布于 2024-08-12 12:26:17 字数 67 浏览 3 评论 0原文

有没有办法可以确定应用程序池(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

长途伴 2024-08-19 12:26:17
DateTime.Now - Process.GetCurrentProcess().StartTime

Process.GetCurrentProcessInfo() 不存在。

DateTime.Now - Process.GetCurrentProcess().StartTime

Process.GetCurrentProcessInfo() doesn't exist.

冷默言语 2024-08-19 12:26:17

非常愚蠢的技巧:在所有东西都使用的某个类中,使用类构造函数来记住您的开始时间并使用 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.

肤浅与狂妄 2024-08-19 12:26:17

在 ASP.NET 应用程序中,您可以尝试 TimeSpan uptime = (DateTime.Now - ProcessInfo.GetCurrentProcessInfo ().StartTime)

From the ASP.NET application, you can try TimeSpan uptime = (DateTime.Now - ProcessInfo.GetCurrentProcessInfo ().StartTime)

这样的小城市 2024-08-19 12:26:17

基于上面的内容,我创建了一个像这样的简单类..

public static class UptimeMonitor
{
    static DateTime StartTime { get; set; }

    static UptimeMonitor()
    {
        StartTime = DateTime.Now;
    }

    public static int UpTimeSeconds
    {
        get { return (int)Math.Round((DateTime.Now - StartTime).TotalSeconds,0); }
    }
}

并在 Global.asax.cs 中的 Application_Start() 中调用它,

var temp = UptimeMonitor.UpTimeSeconds;

然后可以使用它在任何地方访问

UptimeMonitor.UpTimeSeconds

Based on the above I created a simple class like so..

public static class UptimeMonitor
{
    static DateTime StartTime { get; set; }

    static UptimeMonitor()
    {
        StartTime = DateTime.Now;
    }

    public static int UpTimeSeconds
    {
        get { return (int)Math.Round((DateTime.Now - StartTime).TotalSeconds,0); }
    }
}

and called it in Application_Start() in Global.asax.cs like

var temp = UptimeMonitor.UpTimeSeconds;

It can then be accessed anywhere using

UptimeMonitor.UpTimeSeconds
梦初启 2024-08-19 12:26:17

如果您发现 Process.GetCurrentProcessInfo() 不存在,正如其他用户提到的那样,

System.Diagnostics.Process.GetCurrentProcess().StartTime

可能适合您。

(我想将此添加为 Eric Humphrey 帖子的评论,但不允许)

if you find that Process.GetCurrentProcessInfo() doesn't exist as another user mentioned,

System.Diagnostics.Process.GetCurrentProcess().StartTime

may work for you.

(I wanted to add this as a comment to Eric Humphrey's post but I'm not allowed)

烟火散人牵绊 2024-08-19 12:26:17

我个人使用两种方法之一。使用静态类(如@Original10的答案所示)或使用 Application 变量。

我发现使用 Application 变量是可以接受的,因为我注意到 Process.GetCurrentProcess() 在应用程序重新启动后(例如修改 web.config 或 bin 目录)仍然存在。我还需要一些能够满足网站重新启动的需求。

在 Global.asax 中,将以下内容添加到 Application_Start - 并添加该方法(如果不存在)。

public void Application_Start(Object sender, EventArgs e)
{
  ...
  Application["ApplicationStartTime"] = DateTime.Now.ToString("o");
}

在您需要的代码中,您可以执行以下操作:

var appStartTime = DateTime.MinValue;
var appStartTimeValue = Web.HttpCurrent.Application["ApplicationStartTime"].ToString();

DateTime.TryParseExact(appStartTimeValue, "o", null, Globalization.DateTimeStyles.None, Out appStartTime);
var uptime = (DateTime.Now - appStartTime).TotalSeconds

var lsOutput = $"Application has been running since {appStartTime:o} - {uptime:n0} seconds."

这将产生类似于以下内容的内容

Application has been running since 2018-02-16T10:00:56.4370974+00:00 - 10,166 seconds.

:如果需要,不会检查应用程序变量或锁定应用程序。我将把这个作为练习留给用户。

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 noticed Process.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.

public void Application_Start(Object sender, EventArgs e)
{
  ...
  Application["ApplicationStartTime"] = DateTime.Now.ToString("o");
}

In your code where you need it, you could do something like:

var appStartTime = DateTime.MinValue;
var appStartTimeValue = Web.HttpCurrent.Application["ApplicationStartTime"].ToString();

DateTime.TryParseExact(appStartTimeValue, "o", null, Globalization.DateTimeStyles.None, Out appStartTime);
var uptime = (DateTime.Now - appStartTime).TotalSeconds

var lsOutput = 
quot;Application has been running since {appStartTime:o} - {uptime:n0} seconds."

Which will produce something along the lines of

Application has been running since 2018-02-16T10:00:56.4370974+00:00 - 10,166 seconds.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文