tags:

views:

205

answers:

1

By design Log4net only outputs a message to the console if it can't be configured correctly (http://log4net.sourceforge.net/release/1.2.0.30316/doc/manual/faq.html#reliable).

Logging is a critical part of my application, so want to know if Log4net was configured correctly. I've not been able to find any way to get the info from Log4net, so I wrote this code. But I don't like the strategy at all. Any better suggestions?

        TextWriter errorConsole = new StringWriter();
        TextWriter defaultErrorConsole = Console.Error;
        Console.SetError(errorConsole);
        try
        {
            //configure Log4net

            string errorMessage = errorConsole.ToString();
            bool initializationError = errorMessage.IndexOf("log4net", StringComparison.OrdinalIgnoreCase) == -1;
            if (initializationError)
            {
                throw ...
            }
        }
        finally
        {
            Console.SetError(defaultErrorConsole);
        }

Also when logging a message, such as calling ILog.Error. Is there a way to see if it was logged?

+1  A: 

Any errors that occurs during configuration is logged internally in log4net using the log4net.Util.LogLog class. Unfortunately, in the current release this class does not expose very much that can help you. In the current source on the other hand, you can now subscribe to a LogReceived event which fires whenever an internal log message is received.

The logger repository have also been extended with the ILoggerRepository.ConfigurationMessages() method which exposes the latest list of internal messages.

Regarding monitoring whether a message was logged or not: appenders, at least those built on the AppenderSkeleton class, uses the same internal error messaging as used during configuration. So with the latest source you will receive any such notifications via the LogReceived event.

Peter Lillevold
Thanks for your answer. Do you know when a new release is going to be made?
Karsten
That is a good question, which I don't have an answer too :) It is a long outstanding release though. log4net is in active development so we should see a new release sometime...
Peter Lillevold