为什么以下测试会失败呢?(使用xunit)我尝试了不同的appender,但它从未写入任何内容,虽然日志似乎已准备好写入。最终,我创建了自己的appender 来进行测试。
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);
}
}