개발자국/C#

[C#] Exception Dump (Text File)

알 수 없는 사용자 2012. 11. 28. 15:33

호랑이는 죽어서 가죽을 남기고,

사람은 죽어서 슬프고,

Application은 죽어서 Log를 남겨봐야겠다는 생각에..

 

C# 에서의 Exception Dump 비스무리 하게 뭔가를 남겨보려 한다.

 

닷넷에서 지원하는

Application.Current.DispatcherUnhandledException

요런 놈이 존재하더라..

 

뭔고 하니, 어플리케이션에서 Exception이 발생했을 때, 실행할 Action을 등록하면

딱! 하고 보내주는 역할을 한다.

 

예제 소스를 보고나서 설명을 계속하도록 하지..

  1:     public partial class MainWindow : Window
  2:     {
  3:         public MainWindow()
  4:         {
  5:             Application.Current.DispatcherUnhandledException += UnExcept;
  6:             InitializeComponent();
  7:         }
  8: 
  9:         public void UnExcept(object sender, DispatcherUnhandledExceptionEventArgs e)
 10:         {
 11:             using (System.IO.StreamWriter file =
 12:                 new System.IO.StreamWriter(@"C:\"  + string.Format("App_{0:yyyyMMdd}.txt", DateTime.Now), true))
 13:             {
 14:                 file.WriteLine(string.Format("[ {0:yyyy-MM-dd hh:mm s} ] Application Name", DateTime.Now));
 15:                 file.WriteLine("Module : " + e.Exception.Source);
 16:                 file.WriteLine("Message : " + e.Exception.Message);
 17:                 file.WriteLine(e.Exception.StackTrace.ToString());
 18:                 file.WriteLine("\n\n");
 19:                 file.Flush();
 20:                 file.Close();
 21:             }
 22:         }
 23: 
 24:         private void button1_Click(object sender, RoutedEventArgs e)
 25:         {
 26:             int a = 1;
 27:             int b = 0;
 28: 
 29:             int c = a/b;
 30:         }
 31:     }
참 볼품없지.. 나중에 MVVM 강좌를 할 예정인데 맨날 이런 코드만 올리고 있으니..원..

 

뭐 암튼,

MainWindow가 이니셜라이즈 되기 전에, UnExcept 라는 함수를 등록해 두었다.

button1을 클릭하면 말도안되게 0으로 나누라는 명령을 넣어 뒀고,

실행 결과, 0으로 나눌 수 없어서 프로그램이 죽는다.

 

그러나 죽기 직전에 UnExcept 라는 함수를 통과하면서 날짜와 시간이 적힌 txt 파일을 C:\ 에다가

남기게 된다.

 

다른 Dump 남기는 루틴을 넣어도 좋고, 암튼 Exception 발생시 마지막으로 거칠 수 있는 함수를

등록하는 방법이니 알아서 응용하여 사용하기 바란다.

 

txt 파일의 내용은 뭐 알아볼 수 있는건 0으로 나누려 했다는 내용과 위치쯤 되겠지..

 

-------------TXT 파일 내용 ------------------

 

[ 2012-11-28 03:01 9 ] Application Name
Module : WpfApplication9
Message : 0으로 나누려 했습니다.
위치: WpfApplication9.MainWindow.button1_Click(Object sender, RoutedEventArgs e) 파일 F:\Working\WpfApplication9\WpfApplication9\MainWindow.xaml.cs:줄 52
위치: System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
위치: System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
위치: System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
위치: System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
위치: System.Windows.Controls.Primitives.ButtonBase.OnClick()
위치: System.Windows.Controls.Button.OnClick()
위치: System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
위치: System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
위치: System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
위치: System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
위치: System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
위치: System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
위치: System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
위치: System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
위치: System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
위치: System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
위치: System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
위치: System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
위치: System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
위치: System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
위치: System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
위치: System.Windows.Input.InputManager.ProcessStagingArea()
위치: System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
위치: System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
위치: System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
위치: System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
위치: System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
위치: MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
위치: MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
위치: System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
위치: MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)