log4net-LogicalThreadContext-和单元测试用例 [英] log4net - LogicalThreadContext - and unit test cases

查看:195
本文介绍了log4net-LogicalThreadContext-和单元测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am starting to write a unit test (MS Test, with Resharper as the test runner). When I set the LogicalThreadContext (see below), my test cases get 'aborted'. Anybody know why? Is this related to the unit test being on a different thread? How do I resolve this?

[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);
    }

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

The weird thing is that if I were to debug and step through the code, the Assert.AreEqual does get called and passes, so something is happening after that line of code... which is why I think it might have something to do with the test thread, etc.

谢谢!

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

UPDATE: So I ran this test in MSTest and got this exception (Resharper didn't show it)

单元测试适配器引发异常: 成员'log4net.Util.PropertiesDictionary,log4net,版本= 1.2.13.0,区域性=中性,PublicKeyToken = 669e0ddf0bb1aa2a'的类型无法解析.

Unit Test Adapter threw exception: Type is not resolved for member 'log4net.Util.PropertiesDictionary,log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'..

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

I'm using log4net v1.2.13, on VS2013, .Net 4.5.

此链接似乎表明这是一个引用的程序集问题,但没有解决方法.任何其他想法都将受到欢迎,GAC的log4net不是一个选择. https://issues.apache.org/jira/browse/LOG4NET-398

This link seems to suggest it is a referenced assemblies problem, but there is no resolution. Any additional ideas would be greatly welcome, GAC'ing log4net is not an option. https://issues.apache.org/jira/browse/LOG4NET-398

推荐答案

我最终这样做是为了使其正常工作:

I ended up doing this to get it working:

将其放在TestCleanup()方法中:

put this in the TestCleanup() method:

CallContext.FreeNamedDataSlot("log4net.Util.LogicalThreadContextProperties");

这篇关于log4net-LogicalThreadContext-和单元测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆