WinForms 애플리케이션에서 모든 '처리되지 않은'예외를 포착하는 것을 어떻게 만들 수 있습니까?
지금까지 난 그냥 주위에 try / catch 블록을 넣어 Application.Run
에 Program.cs
프로그램에 대한 진입 점. 이것은 디버그 모드에서 모든 예외를 충분히 포착하지만 디버그 모드없이 프로그램을 실행하면 예외가 더 이상 처리되지 않습니다. 처리되지 않은 예외 상자가 나타납니다.
이런 일이 일어나길 원하지 않습니다. 비디 버그 모드에서 실행할 때 모든 예외가 포착되기를 원합니다. 이 프로그램에는 여러 스레드가 있으며 바람직하게는 모든 예외가 동일한 핸들러에 의해 포착됩니다. DB에 예외를 기록하고 싶습니다. 누구든지 이것을 수행하는 방법에 대한 조언이 있습니까?
ThreadException 문서 의 예제를 살펴보십시오 .
public static void Main(string[] args)
{
// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += new
ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);
// Set the unhandled exception mode to force all Windows Forms errors
// to go through our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// Add the event handler for handling non-UI thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
디버깅이 더 쉬워 지므로 디버깅 할 때 예외를 포착하지 않는 것이 좋습니다. 다소 해킹이지만 위의 코드를 다음과 같이 래핑 할 수 있습니다.
if (!AppDomain.CurrentDomain.FriendlyName.EndsWith("vshost.exe")) { ... }
디버깅시 예외 포착을 방지합니다.
NET 4에서는 특정 예외가 더 이상 기본적으로 포착되지 않습니다. 이는 AccessViolationException과 같이 실행 파일의 (치명적일 수있는) 손상된 상태를 나타내는 예외 인 경향이 있습니다.
메인 메소드 앞에 [HandleProcessCorruptedStateExceptions] 태그를 사용해보십시오.
using System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions
[HandleProcessCorruptedStateExceptions]
public static int Main()
{
try
{
// Catch any exceptions leaking out of the program
CallMainProgramLoop();
}
catch (Exception e) // We could be catching anything here
{
System.Console.WriteLine(e.Message);
return 1;
}
return 0;
}
좋은 예는 http://www.csharp-examples.net/catching-unhandled-exceptions/ 에서 찾을 수 있습니다 . 기본적으로 메인을 다음과 같이 변경하십시오.
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.Run(new Form1());
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message, "Unhandled Thread Exception");
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception");
}
이를 위해 NBug 라이브러리를 사용할 수 있습니다 . 다음과 같은 최소한의 설정으로 :
NBug.Settings.Destination1 = "Type=Mail;From=me@mycompany.com;To=bugtracker@mycompany.com;SmtpServer=smtp.mycompany.com;";
AppDomain.CurrentDomain.UnhandledException += NBug.Handler.UnhandledException;
Application.ThreadException += NBug.Handler.ThreadException;
You can start collecting information on all unhandled bugs in your application, even when it's deployed to the clients. If you don't want to use a 3rd party library, you should attach to below events:
// These two should come before enabling visual styles or running the application
AppDomain.CurrentDomain.UnhandledException += ...
Application.ThreadException += ...
...
Application.Run(new Form1());
'your programing' 카테고리의 다른 글
fork ()의 목적은 무엇입니까? (0) | 2020.10.07 |
---|---|
Ruby는 JSON 요청을 보냅니다. (0) | 2020.10.07 |
웹 개발 배우기 : Django vs Node vs Rails vs Others (0) | 2020.10.07 |
Ruby, 경로 + 파일 이름에서 경로 가져 오기 (0) | 2020.10.07 |
Diff 결과를 어떻게 탐색합니까? (0) | 2020.10.07 |