Exception if Log4net is not configured correctly
By design, Log4net only prints 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 an important part of my application, so you want to know if Log4net is configured correctly. I couldn't find a way to get information from Log4net, so I wrote this code. But I don't like this 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 registering a message, such as calling ILog.Error. Is there a way to find out if it's registered?
a source to share
Any errors that occur during configuration are logged inside log4net using the log4net.Util.LogLog class . Unfortunately, in the current release, this class does not cover very much that can help you. In current source , on the other hand, you can now subscribe to the LogReceived event, which fires whenever an internal log message is received.
The log repository has also been extended with the ILoggerRepository.ConfigurationMessages () method, which provides the latest list of internal messages.
Regarding monitoring whether a message was logged or not: appenders, at least the ones created for the AppenderSkeleton class, use the same internal error messaging as they did during setup. So with the last source, you will receive such notifications via the LogReceived event.
a source to share