UnhandledExceptionDispatcher for WPF app

It’s been a while since my previous post about UnhandledExceptionDispatcher in Wpf app. Requests have changed very much, such as:

  1. UnhandledException should be displayed in a nice user friendly window.
  2. UnhandledException should be logged in somewhere.

For 2nd one, I’ve already found the solution by using log4net remotesink. What I need to do is just merging the logging into my logviewer window.

There is a very good exception viewer from the Google result, very nice and handy. My tasks:

  1. Port it into WPF and C#
  2. Remove option of sending and not sending, because logging is happening behind the scene. It will be useful if we are dealing with wide used internet WPF application, for intranet, we don’t need those options.
    public partial class App : Application
    {
        private void OnStartup(object sender, StartupEventArgs e)
        {
            Log.Initialize(new Log4NetLogger(SystemInfo.ApplicationBaseDirectory + "\\Log4Net.config"));
            Log.For(this).Info("Log4Net initialized in app startup.");

            new UnhandledExceptionDispatcher(this, false, true);
            var shell = new Shell();
            shell.Show();
            MainWindow = shell;
            shell.BindViewModel(ObjectFactory.GetInstance<IShellViewModel>());

        }
    public class UnhandledExceptionDispatcher
    {
        private readonly Application _app;
        private readonly bool _handleException;
        public bool RestartApp { get; set; }

        public UnhandledExceptionDispatcher(Application app, bool restartApp, bool handleException)
        {
            _app = app;
            _handleException = handleException;
            RestartApp = restartApp;
            _app.DispatcherUnhandledException += Application_DispatcherUnhandledException;

            Log.For(_app).Info("UnhandledExceptionDispatcher Initialized.");
        }

        public void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {

            Log.For(_app).Error("Application error occurred", e.Exception);
            ShowUnhandledException(e.Exception);
            e.Handled = _handleException;
            if (_app != null) _app.Shutdown(-1);
        }

        private void ShowUnhandledException(Exception e)
        {
            var viewer = new UnhandledExceptionViewer(e, RestartApp);
            //  or use the simple built-in viewer:
            // MessageBox.Show(e.Message, "This Application has encountered a problem");

           if (_app.MainWindow.IsVisible)
                  viewer.Owner = _app.MainWindow;

           viewer.ShowDialog();
     }
 }
Advertisements

Handle/Display UnhandleException in WPF

From MSDN, unhandledExcpetion can be handled genereally in application.xaml,

<Application
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.App"
  StartupUri="MainWindow.xaml"
  DispatcherUnhandledException="App_DispatcherUnhandledException" />

using System.Windows; // Application
using System.Windows.Threading; // DispatcherUnhandledExceptionEventArgs

namespace SDKSample
{
    public partial class App : Application
    {
        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            // Process unhandled exception

...

            // Prevent default unhandled exception processing
            e.Handled = true;
        }
    }
}

I like this very much, but suggestion from IglooCoder is, you don’t want set Handled=true, the UnHandleException should keep unhanded in this UnhandledException event, for specific exception you want to handle, do it on screen or somewhere else, like local WcfProxy. This event should be the place you want to display error or log or send screen shot to admin. Anyway, exceptions shouldn’t be handled here.