Debugging WPF on the client machine

what is the best way to catch wpf application errors. I have already added the following to my 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);
}

But I still get cases where the application crashes and the error does not appear in the message box. How do I debug such errors? The windows event log does not show me any useful information. And of course, the application works fine in the IDE, so that doesn't help either.

+3
source share
1 answer

In the IDE you can catch it. Go to
Debug → Exceptions.

In the dialog box, check the box for the reset Common Language Runtime Exception.

Now you can catch it from the IDE.

NTN

0
source

Source: https://habr.com/ru/post/1760076/


All Articles