Gibraltar Agent and Analyst
Developer's Reference for WinForms - Ensuring Your Application Exits
See Also Send Feedback

Glossary Item Box

Because the Gibraltar 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 Gibraltar 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 Gibraltar 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.

ShowScenario One: Ending the Gibraltar Agent Session Directly

In this scenario the Gibraltar 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 Reference - Agent Configuration through Code.

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

ShowScenario Two: Ending Session without adding any Gibraltar 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 Gibraltar Agent is required.  If you intend to reference the Agent then see Scenario Two for a better approach.

Example WinForms entry point class without Gibraltar 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 Gibraltar 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 Gibraltar 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 Reference
EndSession Method

©2012. All Rights Reserved.