tags:

views:

165

answers:

2

Why in the world does the following test fail? (its in xunit) I've tried it with different appenders and it never writes anything though the log seems like it is ready to write. I eventually created my own appender just to test it.

    public class TestAppender : AppenderSkeleton {
        public event Action<LoggingEvent> AppendCalled = delegate { };
        protected override void Append(LoggingEvent loggingEvent) {
            AppendCalled(loggingEvent);
        }
    }
    public class Class1 {
        private TestAppender _appender = new TestAppender();
        public Class1() {
            log4net.Util.LogLog.InternalDebugging = true;
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
            Logger rootLogger = hierarchy.Root;
            rootLogger.Level = Level.All;
            Logger coreLogger = hierarchy.GetLogger("abc") as Logger;
            coreLogger.Level = Level.All;

            coreLogger.Parent = rootLogger;
            PatternLayout patternLayout = new PatternLayout();
            patternLayout.ConversionPattern = "%logger - %message %newline";
            patternLayout.ActivateOptions();
            _appender.Layout = patternLayout;
            _appender.ActivateOptions();
            coreLogger.AddAppender(_appender);            
        }
        [Fact]
        public void Test() {
            bool called = false;
            _appender.AppendCalled += e => called = true;
            var log = LogManager.GetLogger("abc");
            log.Debug("This is a debugging message");
            Thread.Sleep(TimeSpan.FromSeconds(2));
            log.Info("This is an info message");
            Thread.Sleep(TimeSpan.FromSeconds(2));
            log.Warn("This is a warning message");
            Thread.Sleep(TimeSpan.FromSeconds(2));
            log.Error("This is an error message");
            Assert.True(called);
        }
}
+1  A: 

Just throwing a guess out there...

Do you have the XmlConfiguration defined in your assembly?

Did you forget it in your test project?

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

That usually burns me every once in a while.

Ty
Good call I usually use log4net castle windsor integration so I forgot - but I'm not using an xml file at all!
George Mauer
A: 

Still can't say why the above code doesn't work but I got log4net to configure properly and use my appender by replacing all the configuration code with:

log4net.Config.BasicConfigurator.Configure(_appender);

From here.

George Mauer