Loupe - Log - Monitor - Resolve
Loupe / Getting Started / Adding Loupe to your Application / Developer's Guide for WinForms - Ensuring the Loupe Agent gets Loaded
In This Topic
    Developer's Guide for WinForms - Ensuring the Loupe Agent gets Loaded
    In This Topic

    It's important that the Loupe Agent be initialized as soon as possible in the lifecycle of your application to ensure it can capture information and protect from exceptions.  This can be done by adding a single Trace statement if your application doesn't reference Loupe or by making any Loupe logging call, for example Log.StartSession.

     Scenario One: Initializing the Loupe Agent Directly

    In this scenario the Loupe Agent is called directly.  Configuration is still entirely loaded from the App.Config file; this example is extended to use programmatic configuration in Developer's Guide - Agent Configuration through Code.

    Example WinForms entry class with direct calls to Loupe
    Copy Code
    using Gibraltar.Agent;
    internal static class Program
    {
        [STAThread]
        private static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            // You can initialize the Loupe Agent by just invoking the Agent directly.
            Log.StartSession("Application starting."); // This message only goes to Loupe, not to other Trace listeners.
            // Now, launch the main application form.
            Application.Run(new MainApp());
            Log.EndSession("Application exiting."); // Tell Loupe directly that we're exiting.
        }
    }
    
     Scenario Two: Initialize without adding any Loupe Reference

    In this example only calls to the .NET Trace class are used, and will work with any Trace listener.  No reference in your code the Loupe Agent is required.  If you intend to reference the Agent then see Scenario Two for a better approach.

    Example WinForms entry point class without Loupe reference
    Copy Code
    using System.Diagnostics;
    
    internal static class Program
    {
        [STAThread]
        private static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            // Fire off a message so Trace will scan the app.config (see also) and add Gibraltar's TraceListener.
            // This ensures that the Loupe Agent is loaded and activated, scanning its own part of app.config.
            // Without a message at this point, the Agent would only get loaded when a message is logged some time later,
            // and some of the Agent's automatic features would not be able to function at their best for you.
            Trace.TraceInformation("Application starting.");
            // Nothing else is needed to activate exception handling with Loupe on most winforms apps,
            // as soon as the first line of logging returns above it's active.
            Application.Run(new MainApp());
            Trace.TraceInformation("Application exiting."); // Just for completeness.
            Trace.Close(); // It's always a good idea to close trace when you're exiting the application to have every listener shut down nicely.
        }
    }
    

     The same three trace calls can be used in Windows Services, Console Applications, and WPF applications to ensure the agent gets loaded and exits.