How to get the last start time for an ASP.NET app?

How to get the last time the current ASP.NET app was initiated?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you wish to forego an extra variable, I think this will give you the last time the IIS worker process (a.k.a. application pool) was restarted:

System.Diagnostics.Process.GetCurrentProcess().StartTime

I use it to set cacheability because the page is based on content that I only dynamically generate on application startup:

Response.Cache.SetLastModified(System.Diagnostics.Process.GetCurrentProcess().StartTime);

It is possible to stop/start individual websites within an application pool / worker process, but I infer from this post that doing so will not recreate static application objects, so I gather that the date associated with the worker process is probably the most useful date here.

Keep in mind also that a static application variable created on application startup will actually often times give you the time the application was first visited. It is possible the worker process was started much earlier, and that brings into mind phrases like “IIS application warm up” and “IIS application autostart” and another discussion on when static fields are initialized in general. What you choose might depend on whether or not you’re interested in the last worker process recycle time or if you’re interested in the time other static members were computed.

Method 2

You can set a static field to DateTime.Now in Application_Start in Global.asax.

Method 3

well I guess,create a static class with a static consructor assigning a property to DateTime.Now.

Method 4

Simplest solution:

public static readonly DateTime StartupTime = DateTime.Now;

I would strongly warn anyone from using System.Diagnostics.Process.GetCurrentProcess().StartTime

When you update application files, the ASP.NET app is hot-reloaded within the same process. The App-Domain is being reloaded BUT w3wp process stays the same. Keep that in mind.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x