どこで例外が発生する可能性があるかわからない場合は、アプリケーションでUnhandledExceptionを使用してみてください。
UnhandledException イベントは、メイン UI スレッドからスローされたキャッチされていない例外を処理します。ThreadException イベントは、非 UI スレッドからスローされたキャッチされていない例外を処理します。
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
//... do something ...
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
System.Diagnostics.Trace.WriteLine((e.ExceptionObject as Exception).Message, "Unhandled UI Exception");
// here you can log the exception ...
}
ロギングに MSDN の Trace クラスを使用しました。
System.Diagnostics.Trace
これには、Trace()
メソッドをリッスンし、ログ ファイル/出力ウィンドウ/イベント ログに書き込むリスナーが含まれます。含まれているフレームワークのリスナーはDefaultTraceListener
、TextWriterTraceListener
およびEventLogTraceListener
. レベル (警告、エラー、情報) とカテゴリを指定できます。