I have 100's of unit tests. Most of my logging works fine (i.e., appears in the ReSharper and VS test runner Output windows). However, very oddly, some tests persistently fail to write to the Output Window using log4net (apparently due to 0 appenders). I tracked it down to a private logger field in a referenced class.
Why does the presence of a private Logger field in one class (e.g., FooA) break logging in a different test class (e.g., FooOtherTest)? Is this just a bug in log4net? What is the mechanism of action? (so I can avoid it or workaround it...since it is really hard to track down in code when it occurs due to the levels of indirection).
The 2 files are in 2 different projects (a class library project and a test project). The test project has a log4net.config file w/ Build Action = Content and Copy to Output Directory = Copy Always.
VS2017 Enterprise Version 15.7.4, log4net 2.0.8.0, C# 4.7.1
Here is an illustration that it happens in Visual Studio Test Explorer as well.
Here is the code:
using log4net;
namespace FooBug
{
public static class FooA
{
// toggle to see symptom come and go (excluded = works fine, included = logging in test class fails)
//private static readonly ILog Logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static readonly int fooA = 4;
}
}
using System;
using FooBug;
using log4net;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace OtherTest
{
[TestClass]
public class FooOtherTest
{
private static readonly ILog Logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static readonly int fooA = FooA.fooA;
[TestMethod]
public void Test0()
{
Console.WriteLine(@"sanity check to console");
Logger.Debug("SUCCESS - the logging works right now");
ConsoleWriteAppenders();
}
public static void ConsoleWriteAppenders()
{
var appenders = LogManager.GetRepository().GetAppenders();
Console.WriteLine("appenders.Length = " + appenders.Length);
foreach (var appender in appenders)
{
Console.WriteLine(appender.Name);
}
}
}
}