【发布时间】:2010-08-17 12:39:17
【问题描述】:
在 wpf 应用程序中捕获错误的最佳方法是什么。我已将以下内容添加到我的 app.xaml.cs:
void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendFormat("{0}\n", e.Exception.Message);
stringBuilder.AppendFormat(
"Exception handled on main UI thread {0}.", e.Dispatcher.Thread.ManagedThreadId);
// attempt to save data
var result = MessageBox.Show(
"Application must exit:\n\n" + stringBuilder.ToString() + "\n\nSave before exit?",
"app",
MessageBoxButton.YesNo,
MessageBoxImage.Error);
if (result == MessageBoxResult.Yes)
{
MessageBox.Show(e.Exception.InnerException.ToString());
MessageBox.Show(e.Exception.InnerException.Message.ToString());
}
// Return exit code
this.Shutdown(-1);
// Prevent default unhandled exception processing
e.Handled = true;
}
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = e.ExceptionObject as Exception;
MessageBox.Show(ex.Message, "Uncaught Thread Exception", MessageBoxButton.OK, MessageBoxImage.Error);
}
但我仍然遇到应用程序崩溃并且错误未显示在消息框中的情况。如何调试这些类型的错误? windows的事件日志没有显示任何有用的信息。当然,该应用程序在 IDE 中运行良好,所以这也无济于事。
【问题讨论】:
标签: wpf