During development and debugging of our WPF application some exceptions occurred which were not caught by the code. Such unhandled exceptions are not logged automatically. In order to obtain information about any unhandled exceptions in production and to simplify error analysis, we looked for a way to handle and log unhandled exceptions.

After some research we ended up with the following solution.


To log all unhandled exceptions we implemented an exception handler (CurrentDomain_UnhandledException) in the code behind of App.xaml which gets registered to the AppDomain.CurrentDomain.UnhandledException event in the constructor of App class.

 * Copyright 2017 d-fens GmbH
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

using System;
using System.Configuration;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using biz.dfch.CS.Commons.Diagnostics;
using biz.dfch.CS.ArbitraryWpfApplication.UI.Constants;
using biz.dfch.CS.ArbitraryWpfApplication.UI.Domain.Sewer;
using biz.dfch.CS.ArbitraryWpfApplication.UI.Security;
using TraceSource = biz.dfch.CS.Commons.Diagnostics.TraceSource;

namespace biz.dfch.CS.ArbitraryWpfApplication.UI
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
        internal NavigationService Navigator { get; set; }

        private readonly TraceSource traceSource = Logger.Get(Logging.TraceSourceName.DIGITAL_PROCESS_UI);

        public App()
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

        protected override void OnStartup(StartupEventArgs e)

            traceSource.TraceEvent(TraceEventType.Information, (int)Logging.EventId.Start, Message.App_OnStartup__Initialise_START);

            traceSource.TraceEvent(TraceEventType.Information, (int)Logging.EventId.Stop, Message.App_OnStartup__Initialise_SUCCEEDED);

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            var exception = e.ExceptionObject as Exception;
            if (null != exception)
                traceSource.TraceException(exception, Message.App_CurrentDomain_UnhandledException__UnhandledExceptionOccurred);

        protected override void OnExit(ExitEventArgs e)
            traceSource.TraceEvent(TraceEventType.Information, (int)Logging.EventId.Start, Message.App_OnExit__START);


            traceSource.TraceEvent(TraceEventType.Information, (int)Logging.EventId.Stop, Message.App_OnExit__SUCCEEDED);

        protected override void OnNavigated(NavigationEventArgs e)
            var page = e.Content as Page;

            if (null != page)
                Navigator = page.NavigationService;


That’s it. If an unhandled exception occurs, it gets logged.

« WPF Series -11- Custom MetroDialog WPF Series -13- Advanced Installer »


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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.