처리되지 않은 도메인 예외 및 스레드 예외를 포착하는 이벤트 처리기를 설정했다고 가정합니다. 해당 대리자에서 추적 수신기를 호출하여 예외를 기록 할 수 있습니다. 예외 상황을 설정하기 위해 추가 호출을 실행하기 만하면됩니다.
[STAThread]
private static void Main()
{
// Add the event handler for handling UI thread exceptions
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
// Add the event handler for handling non-UI thread exceptions
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
...
Application.Run(new Form1());
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MyTraceListener.Instance.ExceptionContext = e;
Trace.WriteLine(e.ToString());
}
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
// similar to above CurrentDomain_UnhandledException
}
...
Trace.Listeners.Add(MyTraceListener.Instance);
...
class MyTraceListener : System.Diagnostics.TraceListener
{
...
public Object ExceptionContext { get; set; }
public static MyTraceListener Instance { get { ... } }
}
MyTraceListener의 쓰기 메서드에서 예외 컨텍스트를 가져와 작업 할 수 있습니다. 예외 상황을 동기화하는 것을 잊지 마십시오.