1

In my ASP.NET application that I deploy on Windows Azure I want to log as many useful data as possible. There's Application_End() method:

protected void Application_End(object sender, EventArgs e)
{
}

which is invoked with sender being System.Web.HttpApplicationFactory and e being just System.EventArgs. With such parameters all I can do is just log their types which isn't very useful.

Can I obtain any useful data from these parameters? Are there cases when Application_End() is invoked with parameters that have other - more useful - actual types?

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • What do you mean by *interesting*? What might be interesting for you might not be interesting for me. So could be a little more specific about the information you are looking for in this event? Remember that there's no `HttpContext` when this event is called which narrows down the kind of things you could grab from it. – Darin Dimitrov Jan 25 '13 at 09:31
  • Darin Dimitrov: I have no idea, but maybe I can find the reason why the application ends or anything else that would help me identify the context of the call. – sharptooth Jan 25 '13 at 09:33
  • No, you cannot find out why your application ends. Possible reasons you should be looking for: 1. `web.config` file changed 2. `global.asax` file changed 3. some of the files in the `bin` folder changed or a new file was added or deleted 4. your application hit CPU/memory threshold limits that were defined by the administrator and IIS decided to recycle it in order to free the consumed resources. – Darin Dimitrov Jan 25 '13 at 09:33
  • @Darin Dimitrov: Turns out it is possible, see my answer. – sharptooth Feb 14 '13 at 06:53

1 Answers1

3

IIS, class HttpRuntime included, sources can be downloaded. Careful analysis shows that indeed the event parameters are always the same and convey no useful information. The call stack for the event is also useless - it is always

my Application_End(Object sender, EventArgs e)
at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Web.HttpApplication.InvokeMethodWithAssert(MethodInfo method, Int32 paramCount, Object eventSource, EventArgs eventArgs)
at System.Web.HttpApplication.ProcessSpecialRequest(HttpContext context, MethodInfo method, Int32 paramCount, Object eventSource, EventArgs eventArgs, HttpSessionState session)
at System.Web.HttpApplicationFactory.Dispose()
at System.Web.HttpRuntime.Dispose()
at System.Web.HttpRuntime.ReleaseResourcesAndUnloadAppDomain(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

but... There is System.Web.Hosting.HostingEnvironment.ShutdownReason that can be retrieved from within Application_End() and that is set by HttpRuntime when the application shutdown is being initiated.

So the "interesting data" is System.Web.Hosting.HostingEnvironment.ShutdownReason.

Also see this closely related question

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979