logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From si <sshnug...@gmail.com>
Subject Try XmlConfigurator.Configure() if logging is not working
Date Tue, 09 Oct 2007 06:31:51 GMT

Just spent a few hours struggling to understand why our logging stopped working,
having found the solution, thought I'd post here so it may help others.

We have a log factory which produces log4net.ILog objects, which is really
just a shortcut to the standard approach you see in most examples.

So in our framework assembly we have:

/// <summary>
/// Create log whose name is the calling methods class name.
/// </summary>
/// <returns>Log ready for work.</returns>
/// <remarks>
/// <para>
/// Configures the log repository if it hasn't been configured before.
/// </para>
/// <para>
/// Creates a debug log message right after getting the logger, this follows
/// the log4net recommendation to log first message as early as possible.
/// </para>
/// </remarks>
public static ILog Create()
    ILog log;
    MethodBase method;

    method = new StackTrace().GetFrame(1).GetMethod();
    log = log4net.LogManager.GetLogger(method.DeclaringType);

    if (log4net.LogManager.GetRepository().Configured == false)
        log.Debug("XmlConfigurator now configured");

    log.DebugFormat(CultureInfo.InvariantCulture, "Logging {0}",

    return log;

The new code was the test for "if (...Configured == false)" and then configure.

For some reason, which I haven't been able to determine, as part of a
restructure of our projects and solutions, assemblies other than the
framework assembly wouldn't start logging until XmlConfigurator.Configure()
was called.

So it was working without the call to Configure(), but when I created new
solutions/projects with the new structure, it stopped working.

I couldn't see any differences in the AssemblyInfo.cs on either the framework
assembly or the other assemblies, but either way, I'm now back in business!

Hope that saves someone a few hours of their life :)


View raw message