Loupe - Log - Monitor - Resolve
Loupe / Getting Started / .NET Framework / Using Loupe with WinForms / Developer's Guide - .NET Framework - Ensuring Your Application Exits
In This Topic
    Developer's Guide - .NET Framework - Ensuring Your Application Exits
    In This Topic

    Because the Loupe Agent tries to capture every log message up until the process exits without slowing down your application it can keep your process alive while it flushes information to disk.  Depending on the type of application you are integrating Loupe with you may need to signal the Agent that it's time to shut down so it can switch to synchronous logging mode and not keep the process running.

    Application Type Recommended Action Notes
    ASP.NET No action required The Loupe Agent for ASP.NET automatically detects when the web site is recycling and automatically shuts down the Agent.
    Console End Session Required See below for the options to shutdown the Agent.
    Services End Session Recommended See below for the options to shutdown the Agent.
    WinForms No action required The Agent will detect when your primary user interface thread exits and automatically shut down.
    WPF End Session Recommended See below for the options to shutdown the Agent.

     Scenario One: Ending the Loupe Agent Session 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
    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: Ending Session 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;
    using Gibraltar.Agent;
    
    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.

    See Also

    Developer's Guide