2013-03-05 127 views
3

我有一个项目可能会抛出任何函数和对象的异常,有没有办法在整个框架/程序中捕获任何和所有异常,以便我可以记录它们,以便以后可以看到它们?我想要调用堆栈和异常消息。我不一定知道异常将抛出的位置,但我想记录程序生命周期中任何地方发生的异常。无论如何要做到这一点?我不想尝试捕捉任何可能的异常抛出函数。程序会因为未处理的异常而中断,但我想先记录它。捕获所有异常

+0

这是一个网络,赢的形式或其他类型的项目? – Jason 2013-03-05 23:16:32

+0

Windows桌面应用程序,特别是WCF。 – iefpw 2013-03-05 23:17:08

+0

您的意思是这是一个WCF服务,自行托管在Windows Forms应用程序中? – 2013-03-05 23:40:49

回答

0

你能赶上在应用程序域级别的例外 - 如果你读的链接App Domain Exception

AppDomain currentDomain = AppDomain.CurrentDomain; 
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler); 

它提到一些其他事件可能导致应用程序域不能触发 - 例如ThreadException - 因此可能需要多个处理程序。

Application.ThreadException += new ThreadExceptionEventHandler (ThreadExceptionHandler); 

,并注意以下几点:(有关的WinForms - SetUnhandledExceptionMode

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 
+0

“应用程序”仅适用于winforms。你可能想提一提。 – jgauffin 2013-03-07 12:33:53

+0

好点 - 完成。 – NDJ 2013-03-07 12:43:13

3

是的,有办法做到这一点。 写入以下行主:

// Add the event handler for handling UI thread exceptions to the event. 
Application.ThreadException += new ThreadExceptionEventHandler(MainForm_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); 

,比处理异常

private static void MainForm_UIThreadException(object sender, ThreadExceptionEventArgs t) 
{ 
    //do something 
} 

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 
{ 
    //do something 
} 
0

你总是可以使用AppDomain.UnhandledException

static void Main() 
{ 
    AppDomain currentDomain = AppDomain.CurrentDomain; 
    currentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomain_UnhandledException); 
} 

private static void AppDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args) { 
    Logger.Log((Exception)args.ExceptionObject); 
} 
1

一大看点,捕获异常的处理它们正确地根据抛出的异常的类型。通过拥有一个大的异常处理程序,您无法处理各个异常,而是可能只是在吞咽它们。但这并不是说它不能完成或不应在所有情况下完成。

如果您的项目需要一个大处理程序,那么您可以简单地处理AppDomain.UnhandledException事件。即使你在别处捕捉异常,处理这种方法也是一个好主意,以确保你的程序不会在错过异常时抛出不友好的错误。这假定你正在创建一个WinForm

由于您也使用WCF,您可以查看IErrorHandler接口来帮助处理故障消息。

0

是否有一种方法可以捕获整个 框架/程序中的任何异常,以便日后可以看到它们?

,有桌面应用或Web应用程序来处理未处理的异常处理程序。在桌面应用程序中,该事件称为UnhandledException。在Web应用程序中,它是应用程序入口点上的Application_Error(通常是Global.asax.cs)。

我不想尝试和捕捉任何可能的异常抛出 函数。该程序会因为未处理的异常而中断,但我想要先登录它。

如果你不想捕捉异常,你总是可以简单地重新抛出。它将继续冒泡以破坏程序。

catch(Exception ex) 
{ 
    //logging here 
    throw;//rethrow 
} 

我想调用堆栈和异常信息。

Exception类包含这些。在事件处理程序的情况下,有多种访问方式取决于它是什么类型的应用程序。

1

有没有办法在整个框架/程序中捕获任何和所有异常,以便我可以记录它们以便日后可以看到它们?

捕捉所有未处理的异常为所有应用程序类型的唯一方法是使用已经提到AppDomain.CurrentDomain.UnhandledException。然而,你不能阻止你的应用程序终止使用该事件(嗯,你可以,但我不会告诉你这是怎么回事,因为它有点骇人听闻)。

但是,在大多数框架中都可以捕获未处理的异常,这些异常允许您只是查看异常并继续前进。既然你提到过WCF你可能想看看IErrorHandler

我不想尝试着去捕捉任何可能的异常抛出函数。

这就是我如何做到的。 Do NOT catch that exception。 ;)

+0

不错的链接 - 我会在稍后阅读。 – NDJ 2013-03-07 12:44:04