I’ve been learning and evaluating the Gibraltar ‘Runtime intelligence’ & logging application recently. If you’re already using log4net, there’s a very low impact route to adopting its many benefits by using their simple Gibraltar Appender.

In knocking up a quick sample application to test, I had setup and configured my Log4Net logger:

public class Program
{
  private static readonly ILog Log = 
    LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  static int Main(string[] args)
  {
    if (!LogManager.GetRepository().Configured)
    {
      XmlConfigurator.Configure();
    }

The simple application went on to output some simple log lines to test using the Gibraltar appender. All was fine, except this simple console application appeared to hang on exit. I quit the running application and Gibraltar dutifully announced that my session had Crashed.

A bit of headscratching later made me realise that I needed to be a better Log4Net citizen in my sample application. I had omitted the line:

Log.Logger.Repository.Shutdown();

Now my code properly stopped its logging activities before the program exited and Gibraltar was able to report successfully. The Log4Net RollingFileAppender or ConsoleAppenders don’t complain like this on program exit if the repository isn’t shutdown first but it does makes sense to tidy up after yourself rather than relying on the garbage collector.

I’ll write more about my positive experiences of Gibraltar in another post, but just wanted to share this in case any early adopters faced the same ‘facepalm’.

Tagged with:
 

2 Responses to Shutting down log4net repositories

  1. Jay Cincotta says:

    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.

    This is covered in more detail in our product documentation:
    http://www.gibraltarsoftware.com/Support/Documentation.aspx?Page=WinForms_DevelopersReference_EnsureYourApplicationExits.html

    Short version: Gibraltar usually can detect application automatically but you can always ensure proper application exit by calling Gibraltar.Agent.Log.EndSession
    when the application is ready to end.

  2. Jonathan says:

    Thanks for the comment, Jay.
    The program is just a Console app run in Visual Studio 2010 compiled against the full .NET Framework 4 and is listed below. Your suggestion of Gibraltar.Agent.Log.EndSession works just fine, as well as the Log.Logger.Repository.Shutdown I mentioned in my original post.
    I guess I didn’t look in that section of your excellent documentation as I didn’t consider my Console application a ‘WinForms’ app?

    If neither of the two ways of ending the logging is called, the console application hangs for at least a minute before I click Stop in Visual Studio and get a Crashed status reported in the Gibraltar Agent.
    Still enjoying trialling Gibraltar. Thanks for all the help and support so far.


    using System;
    using System.Reflection;
    using System.Threading;
    using log4net;
    using log4net.Config;

    namespace SampleLogger
    {
    public class Program
    {
    private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    static int Main(string[] args)
    {
    if (!LogManager.GetRepository().Configured)
    {
    XmlConfigurator.Configure();
    }

    var programRunning = true;
    var i = 0;

    while (programRunning)
    {
    Console.WriteLine("Hello " + i);
    Log.Debug("Hello " + i);
    i++;
    Thread.Sleep(1000);
    if (i > 10) programRunning = false;
    }

    Gibraltar.Agent.Log.EndSession();

    //Log.Logger.Repository.Shutdown();
    return 0;
    }
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>