7

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#?

Grace Note
  • 3,205
  • 4
  • 35
  • 55
JPero
  • 1,272
  • 16
  • 21

7 Answers7

9
DateTime.Now - Process.GetCurrentProcess().StartTime

Process.GetCurrentProcessInfo() doesn't exist.

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Eric Humphrey
  • 197
  • 1
  • 3
6

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.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • By class constructor do you mean `static` constructor? Because the static constructor is what you'd want to do with this, I've never considered how easily you could use a static constructor to achieve something like this before though. – Chris Marisic May 27 '10 at 20:59
  • Yeah class constructor in IL = static constructor in C#. – Joshua May 27 '10 at 23:42
  • The AppDomain can be recycled (change web.config) without the app pool being recycled (w3wp.exe process). This static constructor will only measure latest appdomain restart. Use process info answers (@EricHumphrey) if you really need the app pool process start time. – yzorg Feb 28 '13 at 05:42
  • True, but I found when asking this question, the AppDomain is almost always what you want. – Joshua Feb 28 '13 at 16:31
3

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

Gonzalo
  • 20,805
  • 3
  • 75
  • 78
3

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
Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Original10
  • 572
  • 3
  • 8
2

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)

and... break
  • 478
  • 4
  • 5
1

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 = $"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.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
0

If you mashed Restarting (Recycling) an Application Pool and http://forums.iis.net/t/1162615.aspx, you should get it

Community
  • 1
  • 1
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249