Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 506
Jader Dias Avatar asked Feb 14 '11 15:02

Jader Dias


2 Answers

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.

like image 146
scradam Avatar answered Sep 18 '22 12:09

scradam


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

like image 34
SLaks Avatar answered Sep 21 '22 12:09

SLaks