Chat Support
Loupe - Log - Monitor - Resolve
Loupe / Getting Started / .NET Framework / Using Loupe with WPF / Using Loupe with WPF - Integration Code
In This Topic
    Using Loupe with WPF - Integration Code
    In This Topic

    Because of the design of WPF and the fact that the Loupe Agent targets .NET 2.0 it can't automatically bind to all of the necessary items within WPF for the best experience.  It's recommended you follow the instructions below to make the key attachments necessary for all of the features of the Agent to work at their best.

    Unhandled Exception Handling

    To connect the Loupe Error Manager to the WPF Dispatcher you need to:

    1. Add an event handler to the application DispatcherUnhandledException event
    2. Forward the exception into the Loupe Error Manager.
    Example Dispatcher Unhandled Exception Event Handler
    Copy Code
    using Gibraltar.Agent;
    private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        Log.ReportException(e.Exception, "Test Application", true, false);
        e.Handled = true;
    }
    

    Ensuring the Agent Starts with the Application

    You want to make sure the Loupe Agent starts as early as feasible with your application so it can monitor performance, provide exception handling, and give an accurate timeline of the application.  To do this:

    1. Add an event handler to the application Startup event
    2. Call Log.StartSession during this event.

    The following sample demonstrates this as well as shows how to connect to the Log.Initializing event to enable programmatic configuration and the Log.MessageAlert event for automatic submission of data on an error.

    Application Startup Event Handler and Related Code
    Copy Code
    using Gibraltar.Agent;
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        //Lets connect to the Log Initializing event so we can do programmatic configuration.
        //You can also just use your app.config file, but many people prefer the more powerful approach
        Log.Initializing += new Log.InitializingEventHandler(Log_Initializing);
        //Make sure the Loupe Agent is spun up right now (we're the entry point to the app)
        Log.StartSession("Starting WPF Sample");
        //just like in WinForms we need to add some visual style for the Loupe WinForms dialogs to look nice.
        System.Windows.Forms.Application.EnableVisualStyles();
    }
    void Log_Initializing(object sender, LogInitializingEventArgs e)
    {
        e.Configuration.Viewer.FormTitleText = "Live Log Viewer (WinForms within WPF)";
    }
    

    Ensuring the Application Exits Correctly

    Because the Loupe Agent buffers data and performs operations in the background for best application performance it has to be notified when the application wants to exit so it can enter a shutdown mode.  The best way to do this in WPF is to:

    1. Add an event handler to the application Exit event
    2. Call Log.EndSession during this event.
    Application Exit Event Handler
    Copy Code
    using Gibraltar.Agent;
    private void Application_Exit(object sender, ExitEventArgs e)
    {
        //This call ensures that the Agent will prepare to exit and close out any open windows.
        Log.EndSession("Ending WPF Sample Normally");
    } 
    

    Bringing it All Together

    The following sample shows a standard application XAML file with event handlers registered for startup, exit, and exception handling

    Example Application XAML
    Copy Code
    <Application x:Class="AgentTest.Wpf.App"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            StartupUri="MainWindow.xaml"
            DispatcherUnhandledException="Application_DispatcherUnhandledException"
            Exit="Application_Exit"
            Startup="Application_Startup">
        <Application.Resources>
            
        </Application.Resources>
    </Application>
    

    The following code sample goes in the codebehind file for the XAML sample above to implement the various event handlers.

    Example Application Class
    Copy Code
    using Gibraltar.Agent;
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            Log.ReportException(e.Exception, "Test Application", true, false);
            e.Handled = true;
        }
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            //Lets connect to the Log Initializing event so we can do programmatic configuration.
            //You can also just use your app.config file, but many people prefer the more powerful approach
            Log.Initializing += new Log.InitializingEventHandler(Log_Initializing);
            //Make sure the Loupe Agent is spun up right now (we're the entry point to the app)
            Log.StartSession("Starting WPF Sample");
            //just like in WinForms we need to add some visual style for the Loupe WinForms dialogs to look nice.
            System.Windows.Forms.Application.EnableVisualStyles();
        }
        private void Application_Exit(object sender, ExitEventArgs e)
        {
            //This call ensures that the Agent will prepare to exit and close out any open windows.
            Log.EndSession("Ending WPF Sample Normally");
        }
        void Log_Initializing(object sender, LogInitializingEventArgs e)
        {
            e.Configuration.Viewer.FormTitleText = "Live Log Viewer (WinForms within WPF)";
        }
    }
    

    Going Deeper - Integrate Live Viewer & Packager

    Although they are built using WinForms, both the Loupe Live Viewer and Packager Dialog work great with WPF.  To see examples of how to use them, see:

    See Also