Is there any interesting data I could obtain from

2019-08-08 18:13发布

问题:

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?

回答1:

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