问题遇到的现象和发生背景
用一个窗体程序调用类库,可以正常输出,但是用c#的单元测试来调用类库就出错。
问题相关代码,请勿粘贴截图
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyBase;
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
namespace TestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
log4net.Config.XmlConfigurator.Configure(); // log4net
Class1 c = new Class1();
c.TestLog();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyBase
{
public class Class1
{
public void TestLog()
{
LogHelper.WriteSysLog("Class1.TestLog()");
}
}
}
运行结果及报错内容
代码:log4net.Config.XmlConfigurator.Configure(); 执行异常,信息如下:
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'log4net.Core.LoggerManager' threw an exception.
Source=log4net
StackTrace:
at log4net.Core.LoggerManager.GetRepository(Assembly repositoryAssembly)
at log4net.LogManager.GetRepository(Assembly repositoryAssembly)
at log4net.Config.XmlConfigurator.Configure()
at TestProject1.UnitTest1.TestMethod1() in C:...TestProject1\UnitTest1.cs:line 16
内部异常 1:
TypeInitializationException: The type initializer for 'log4net.Util.SystemInfo' threw an exception.
内部异常 2:
FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. 系统找不到指定的文件。
我的解答思路和尝试过的方法
1、检查过,测试程序的同级目录有log4net.config文件;
2、原来用FORM程序,调用类库可以正常输出日志。
我想要达到的结果
希望能够单元测试带有log4net的类库。