본문 바로가기
Web Program/Asp.net Lecture

UnhandledExceptionModule.cs

by 현이빈이 2010. 5. 11.
반응형
IIS 가 죽는다.

이벤트 뷰어를 보니 아래 로그가 나타난다.
응용 프로그램 풀 ‘DefaultAppPool’에 사용되는 프로세스가 예기치 않게 종료되었습니다. 프로세스 ID는 ‘2548’입니다. 프로세스 종료 코드는 ‘0xe0434f4d’입니다.

검색을 해 보니.. 웹응용프로그램에서 다수의 오류가 발생될경우 나타나는 현상이란다.
다른 URL 을 호출하는 코드가 있는데. 받는 URL 에서 에러를 발생 시켜서 나는 오류 같다.

MSDN 을 검색해 보니 해결 방법이 나와 있다.

아직 적용은 해 보지 않아 정확히 작동이 되는지 여부는 모르겠다.

자세한 내용은 아래 사이트 참조
http://support.microsoft.com/kb/911816/ko


MSDN 본문 내용


예외 정보를 응용 프로그램 로그에 기록하도록 IHttpModule 개체의 소스 코드를 수정합니다. 다음과 같은 정보가 기록됩니다.
  • 예외가 발생한 가상 디렉터리 경로
  • 예외 이름
  • 메시지
  • 스택 추적
IHttpModule 개체를 수정하려면 다음과 같이 하십시오.

참고 이 코드는 이벤트 종류가 오류이고 이벤트 원본이 ASP.NET 2.0.50727.0인 메시지를 응용 프로그램 로그에 기록합니다. 모듈을 테스트하려면 ThreadPool.QueueUserWorkItem 메서드를 사용하는 ASP.NET 페이지를 요청하여 처리되지 않은 예외를 발생시키는 메서드를 호출합니다.
  1. 다음 코드를 UnhandledExceptionModule.cs 파일에 삽입합니다.
    using System;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    using System.Web;
     
    namespace WebMonitor {
        public class UnhandledExceptionModule: IHttpModule {
    
            static int _unhandledExceptionCount = 0;
    
            static string _sourceName = null;
            static object _initLock = new object();
            static bool _initialized = false;
    
            public void Init(HttpApplication app) {
    
                // Do this one time for each AppDomain.
                if (!_initialized) {
                    lock (_initLock) {
                        if (!_initialized) { 
    
                            string webenginePath = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "webengine.dll"); 
    
                            if (!File.Exists(webenginePath)) {
                                throw new Exception(String.Format(CultureInfo.InvariantCulture,
                                                                  "Failed to locate webengine.dll at '{0}'.  This module requires .NET Framework 2.0.", 
                                                                  webenginePath));
                            } 
    
                            FileVersionInfo ver = FileVersionInfo.GetVersionInfo(webenginePath);
                            _sourceName = string.Format(CultureInfo.InvariantCulture, "ASP.NET {0}.{1}.{2}.0",
                                                        ver.FileMajorPart, ver.FileMinorPart, ver.FileBuildPart);
    
                            if (!EventLog.SourceExists(_sourceName)) {
                                throw new Exception(String.Format(CultureInfo.InvariantCulture,
                                                                  "There is no EventLog source named '{0}'. This module requires .NET Framework 2.0.", 
                                                                  _sourceName));
                            }
     
                            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
     
                            _initialized = true;
                        }
                    }
                }
            }
    
            public void Dispose() {
            }
    
            void OnUnhandledException(object o, UnhandledExceptionEventArgs e) {
                // Let this occur one time for each AppDomain.
                if (Interlocked.Exchange(ref _unhandledExceptionCount, 1) != 0)
                    return;
    
                StringBuilder message = new StringBuilder("\r\n\r\nUnhandledException logged by UnhandledExceptionModule.dll:\r\n\r\nappId=");
    
                string appId = (string) AppDomain.CurrentDomain.GetData(".appId");
                if (appId != null) {
                    message.Append(appId);
                }
                
    
                Exception currentException = null;
                for (currentException = (Exception)e.ExceptionObject; currentException != null; currentException = currentException.InnerException) {
                    message.AppendFormat("\r\n\r\ntype={0}\r\n\r\nmessage={1}\r\n\r\nstack=\r\n{2}\r\n\r\n",
                                         currentException.GetType().FullName, 
                                         currentException.Message,
                                         currentException.StackTrace);
                }           
    
                EventLog Log = new EventLog();
                Log.Source = _sourceName;
                Log.WriteEntry(message.ToString(), EventLogEntryType.Error);
            }
    
        }
    }
  2. UnhandledExceptionModule.cs 파일을 다음 폴더에 저장합니다.
    C:\Program Files\Microsoft Visual Studio 8\VC
  3. Microsoft Visual Studio 2005 명령 프롬프트를 엽니다.
  4. sn.exe -k key.snk를 입력하고 Enter 키를 누릅니다.
  5. csc /t:library /r:system.web.dll,system.dll /keyfile:key.snk UnhandledExceptionModule.cs를 입력하고 Enter 키를 누릅니다.
  6. gacutil.exe /if UnhandledExceptionModule.dll을 입력하고 Enter 키를 누릅니다.
  7. ngen install UnhandledExceptionModule.dll을 입력하고 Enter 키를 누릅니다.
  8. gacutil /l UnhandledExceptionModule을 입력하고 Enter 키를 눌러 UnhandledExceptionModule 파일의 강력한 이름을 표시합니다.
  9. 다음 코드를 ASP.NET 기반 응용 프로그램의 Web.config 파일에 추가합니다.
    <add name="UnhandledExceptionModule" 
    	type="WebMonitor.UnhandledExceptionModule, <strong name>" />
반응형