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#?
-
Externally, or within your ASP.NET app? – Ruben Bartelink Nov 23 '09 at 16:07
-
Externally. I've been playing around with the Microsoft.Web.Administration.ApplicationPool, but I don't see anything as far as time. – JPero Nov 23 '09 at 16:15
7 Answers
DateTime.Now - Process.GetCurrentProcess().StartTime
Process.GetCurrentProcessInfo()
doesn't exist.

- 23,328
- 24
- 73
- 116

- 197
- 1
- 3
-
Interestingly, this survives an application restart, so I'm not sure how accurate this is. – Dan Atkinson Feb 16 '18 at 14:35
-
This gets the start time of the w3wp process ( IIS Service ) and not the application pool. – Alex Jul 12 '18 at 12:41
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.

- 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
-
-
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
From the ASP.NET application, you can try TimeSpan uptime = (DateTime.Now - ProcessInfo.GetCurrentProcessInfo ().StartTime)

- 20,805
- 3
- 75
- 78
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

- 11,391
- 14
- 81
- 114

- 572
- 3
- 8
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)

- 478
- 4
- 5
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.

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

- 1
- 1

- 59,778
- 26
- 187
- 249