Shutting down log4net repositories

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’.

3 comments

  1. 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.

    1. 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;
      }
      }
      }

  2. Hi, I think the goal is to always find the balance between quality and quantity. A blog that’s full of quality posts matters more in my opinion to one that is updated every day.
    There are those who realised early on that search engines promoted sites who were active and changing all the time. However, I’ve found that when people find a post you’ve made useful and link to it from other sites, that can drive more traffic to your site than relying on just a search engine to drive people to your content. Pinterest.com is a great example of this at work because pinned items don’t display the date when they were pinned; it’s all about the content. So a ‘pin’ on Pinterest can keep generating blog views long after you’ve written it.

    So focus on the quality of your writing and don’t get hung up about needing to write every day if you’ve not got something of similar quality to share. If you’ve got that much content to write about, go for it!

    If there’s any more specifics you’d like to know, drop us a note here and I’ll endeavour to reply.
    Best wishes with your blog!

Leave a comment

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