0
log4netを設定して未処理の例外をログに記録したいと考えています。 設定ファイルを変更するだけで達成できますか?はいの場合、それをどうやって行うのですか? それが不可能な場合は、これを実現するためにコードに何を実装する必要がありますか?Log4Netで未処理の例外をログに記録する方法
log4netを設定して未処理の例外をログに記録したいと考えています。 設定ファイルを変更するだけで達成できますか?はいの場合、それをどうやって行うのですか? それが不可能な場合は、これを実現するためにコードに何を実装する必要がありますか?Log4Netで未処理の例外をログに記録する方法
設定でのみ行うことはできません。実際の例外をどこかに記録する必要があります。だから、あなたは 'キャッチされていない'例外をキャッチし、それを記録する必要があります。
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);
}
ハンドラーでは、log4netにログすることができます。その後、設定でスイッチをオン/オフすることができます。
ただし、例外がキャッチされない場合があります。たとえば、フェイルファーストが呼び出されたときなどです。
さまざまな種類のアプリケーションでは、未処理の例外ハンドラを設定するさまざまな方法があります。このハンドラを使用して例外をlog4netに記録できます – stuartd