log4net - LogicalThreadContext - 和单元测试用例

Ray*_*ond 22 log4net multithreading unit-testing

我开始编写单元测试(MS测试,Resharper作为测试运行器).当我设置LogicalThreadContext(见下文)时,我的测试用例被"中止".谁知道为什么?这与单元测试在不同的线程上有关吗?我该如何解决这个问题?

[TestClass]
public class ContextInfoTest
{
    private ILog _log;


    [TestInitialize]
    public void TestInitialize()
    {
        // logging configured in assembly.info
        _log = LogManager.GetLogger(this.GetType()); 
    }


    [TestMethod]
    public void FigureOutWhyAborting()
    {
        string input = "blah";
        LogicalThreadContext.Properties["mypropertyname"] = input;

        string output = LogicalThreadContext.Properties["mypropertyname"] as string;
        Assert.AreEqual(input, output);
    }


    [TestMethod]
    public void ThisWorks()
    {
        string input = "blah";
        CallContext.LogicalSetData("mypropertyname", input);

        string output = CallContext.LogicalGetData("mypropertyname") as string;
        Assert.AreEqual(input, output);
    }
Run Code Online (Sandbox Code Playgroud)

奇怪的是,如果我要调试并逐步执行代码,Assert.AreEqual会被调用并传递,因此在该行代码之后发生了一些事情...这就是为什么我认为它可能与某些事情有关测试线程等

谢谢!

更新:所以我在MSTest中运行了这个测试并得到了这个例外(Resharper没有显示它)

单元测试适配器抛出异常:成员'log4net.Util.PropertiesDictionary,log4net,Version = 1.2.13.0,Culture = neutral,PublicKeyToken = 669e0ddf0bb1aa2a'的类型未解析..

我在VS2013,.Net 4.5上使用log4net v1.2.13.

这个链接似乎暗示它是一个引用的程序集问题,但没有解决方案.非常欢迎任何其他想法,GAC'log4net不是一个选项. https://issues.apache.org/jira/browse/LOG4NET-398

Ray*_*ond 32

我最终这样做是为了让它工作:

把它放在TestCleanup()方法中:

CallContext.FreeNamedDataSlot("log4net.Util.LogicalThreadContextProperties");
Run Code Online (Sandbox Code Playgroud)