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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s