작업자 프로세스(w3wp.exe)가 재시작되는 시점을 알 수 있는 방법 - 두 번째 이야기
예전 글에 이어,
작업자 프로세스(w3wp.exe)가 재시작되는 시점을 알 수 있는 방법
; https://www.sysnet.pe.kr/2/0/841
아래의 글은 Global.asax의 Application_End 이벤트 핸들러에서 Recycle의 원인을 알아내는 방법을 설명합니다.
Logging ASP.NET Application Shutdown Events
; https://weblogs.asp.net/scottgu/433194
정리하면, Application_End가 호출되는 시점에 HttpRuntime 객체에 저장된 _shutDownMessage와 _shutDownStack 필드의 값을 .NET Reflection을 이용해 읽어내는 것입니다.
protected void Application_End(object sender, EventArgs e)
{
HttpRuntime runtime = (HttpRuntime)typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime",
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);
if (runtime == null)
return;
string shutDownMessage = (string)runtime.GetType().InvokeMember("_shutDownMessage",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null);
string shutDownStack = (string)runtime.GetType().InvokeMember("_shutDownStack",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null);
System.Diagnostics.Trace.WriteLine(String.Format("\r\n\r\n_shutDownMessage={0}\r\n\r\n_shutDownStack={1}",
shutDownMessage, shutDownStack));
}
위의 예제는 ASP.NET 2.0을 대상으로 하지만, .NET 4.6.1에서도 여전히 잘 동작합니다. 가령, web.config을 변경한 경우 .NET 2.0 기반에서는 다음과 같이 출력 값이 나오고,
_shutDownMessage=CONFIG change
HostingEnvironment initiated shutdown
CONFIG change
CONFIG change
HostingEnvironment caused shutdown
_shutDownStack= at System.Environment.get_StackTrace()
at System.Web.Hosting.HostingEnvironment.InitiateShutdownInternal()
at System.Web.Hosting.HostingEnvironment.InitiateShutdown()
at System.Web.HttpRuntime.ShutdownAppDomain(String stackTrace)
at System.Web.Configuration.HttpConfigurationSystem.OnConfigurationChanged(Object sender, InternalConfigEventArgs e)
at System.Configuration.BaseConfigurationRecord.OnStreamChanged(String streamname)
.NET 4.6.1에서는 이렇게 나옵니다.
_shutDownMessage=IIS configuration change
CONFIG change
HostingEnvironment initiated shutdown
CONFIG change
CONFIG change
HostingEnvironment caused shutdown
_shutDownStack= at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at System.Web.Hosting.HostingEnvironment.InitiateShutdownInternal()
at System.Web.Hosting.HostingEnvironment.InitiateShutdownWithoutDemand()
at System.Web.Hosting.PipelineRuntime.StopProcessing()
그런데, Application_End는 Global.asax 이외의 클래스에서는 구독하는 것이 안 됩니다. 왜냐하면, End 이벤트가 HttpApplication 객체에는 없기 때문입니다. 따라서 외부 DLL에서 참조하고 싶다면 다음과 같이 HttpContext를 이용해 Disposed 이벤트를 구독해야 합니다.
HttpContext.Current.ApplicationInstance.Disposed += ApplicationInstance_Disposed;
Application_End와 Disposed가 이벤트가 발생하는 시점을 잡아 각각의 Call stack을 확인하면 그 이유를 알 수 있습니다.
// Application_End 호출 시점의 Call stack
> Test.dll!WebSiteTest.Global.Application_End(object sender, System.EventArgs e) Line 430 C#
[Native to Managed Transition]
System.Web.dll!System.Web.HttpApplication.InvokeMethodWithAssert(System.Reflection.MethodInfo method, int paramCount, object eventSource, System.EventArgs eventArgs) + 0x7a bytes
System.Web.dll!System.Web.HttpApplication.ProcessSpecialRequest(System.Web.HttpContext context, System.Reflection.MethodInfo method, int paramCount, object eventSource, System.EventArgs eventArgs, System.Web.SessionState.HttpSessionState session) + 0x126 bytes
System.Web.dll!System.Web.HttpApplicationFactory.FireApplicationOnEnd() + 0x6b bytes
System.Web.dll!System.Web.HttpApplicationFactory.Dispose() + 0x80 bytes
System.Web.dll!System.Web.HttpRuntime.Dispose() + 0x158 bytes
System.Web.dll!System.Web.HttpRuntime.ReleaseResourcesAndUnloadAppDomain(object state) + 0x3d bytes
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x15e bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x17 bytes
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() + 0x70 bytes
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() + 0x160 bytes
[Native to Managed Transition]
[Appdomain Transition]
[Native to Managed Transition]
// Global_Disposed 호출 시점의 Call stack
> Test.dll!WebSiteTest.Global.Global_Disposed(object sender, System.EventArgs e) Line 425 C#
System.Web.dll!System.Web.HttpApplication.Dispose() + 0x9f bytes
System.Web.dll!System.Web.HttpApplication.DisposeInternal() + 0x30 bytes
System.Web.dll!System.Web.HttpApplicationFactory.DisposeHttpApplicationInstances(System.Collections.Stack freeList, ref int numFreeInstances) + 0xf6 bytes
System.Web.dll!System.Web.HttpApplicationFactory.Dispose() + 0xd2 bytes
System.Web.dll!System.Web.HttpRuntime.Dispose() + 0x158 bytes
System.Web.dll!System.Web.HttpRuntime.ReleaseResourcesAndUnloadAppDomain(object state) + 0x3d bytes
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x15e bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x17 bytes
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() + 0x70 bytes
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() + 0x160 bytes
[Native to Managed Transition]
[Appdomain Transition]
[Native to Managed Transition]
어차피 HttpRuntime.Dispose 메서드를 시작으로 Application_End가 호출된 후 Disposed 이벤트가 발생하기 때문에 HttpRuntime의 _shutDownMessage와 _shutDownStack 필드 값은 동일합니다.
(
첨부 파일은 이 글의 예제 코드를 포함합니다.)
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]