gpt4 book ai didi

multithreading - log4net - LogicalThreadContext - 和单元测试用例

转载 作者:行者123 更新时间:2023-12-03 11:39:45 26 4
gpt4 key购买 nike

我开始编写单元测试(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);
}

奇怪的是,如果我要调试和单步执行代码, 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'ing log4net 不是一个选项。
https://issues.apache.org/jira/browse/LOG4NET-398

最佳答案

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

把它放在 TestCleanup() 方法中:

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

关于multithreading - log4net - LogicalThreadContext - 和单元测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23661372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com