如何将Log4net指向其配置文件?
我们最近将所有测试从 NUnit 转移到了 MsTest。我们使用 Spring.NET 作为 IOC 容器和 Log4Net。尽管测试项目中有 log4net.xml,但 Log4Net 无法找到它。有什么想法吗?
这是我们收到的错误:
无法创建类的实例 OurCompany.DataAccess.DocumentManagement.Tests.EmailVerificationTokensAdapterTests.DeleteTests。 错误:Common.Logging.ConfigurationException:无法创建 Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter 类型的实例。 可能的解释是缺少零参数和单个参数 NameValueCollection 构造函数 ---> System.Reflection.TargetInitationException:已引发异常 通过调用的目标。 ---> Common.Logging.ConfigurationException:log4net 配置文件 “log4net.xml”不存在。在 Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter..ctor(NameValueCollection 特性) --- 内部异常堆栈跟踪结束 --- 在 System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo 方法,Object[] args,ref SignatureStruct 签名,RuntimeType 声明类型)位于 System.RuntimeMethodHandle.InvokeConstructor(IRuntimeMethodInfo 方法、Object[] args、SignatureStruct 签名、RuntimeType 声明类型)位于 System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr、Binder 绑定器、Object[] 参数、CultureInfo 文化) 在 System.RuntimeType.CreateInstanceImpl(BindingFlags 绑定Attr, Binder 活页夹,Object[] args,CultureInfo 文化,Object[] 激活属性)在 System.Activator.CreateInstance(类型类型, BindingFlags bindingAttr、活页夹活页夹、Object[] args、CultureInfo 文化,对象[]激活属性)在 Common.Logging.LogManager.BuildLoggerFactoryAdapter() --- 内部异常堆栈跟踪结束 --- 在 Common.Logging.LogManager.BuildLoggerFactoryAdapter() 处 Common.Logging.LogManager.get_Adapter() 位于 Spring.Testing.Microsoft.AbstractSpringContextTests..ctor() 在 Spring.Testing.Microsoft.AbstractTransactionalDbProviderSpringContextTests..ctor() 在 OurCompany.Tests.Common.Domain.SATransactionalIntegrationTestsBase..ctor() 在 SATransactionalIntegrationTestsBase.cs 中:第 19 行 OurCompany.DataAccess.DocumentManagement.Tests.EmailVerificationTokensAdapterTests.DeleteTests..ctor() 在DeleteTests.cs中:第20行
这是我们在配置文件中的内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.TraceLoggerFactoryAdapter, Common.Logging">
<arg key="level" value="All" />
<arg key="showLogName" value="true" />
<arg key="showDataTime" value="true" />
<arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
</factoryAdapter>
</logging>
</common>
</configuration>
We recently moved all our tests from NUnit to MsTest. We are using Spring.NET for an IOC container and Log4Net. Log4Net is unable to find the log4net.xml despite having it in test project. Any ideas what it could be?
Here is the error that we are receiving:
Unable to create instance of class
OurCompany.DataAccess.DocumentManagement.Tests.EmailVerificationTokensAdapterTests.DeleteTests.
Error: Common.Logging.ConfigurationException: Unable to create
instance of type Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter.
Possible explanation is lack of zero arg and single arg
NameValueCollection constructors --->
System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. --->
Common.Logging.ConfigurationException: log4net configuration file
'log4net.xml' does not exists. at
Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter..ctor(NameValueCollection
properties)
--- End of inner exception stack trace --- at System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo
method, Object[] args, ref SignatureStruct signature, RuntimeType
declaringType) at
System.RuntimeMethodHandle.InvokeConstructor(IRuntimeMethodInfo
method, Object[] args, SignatureStruct signature, RuntimeType
declaringType) at
System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr,
Binder binder, Object[] args, CultureInfo culture, Object[]
activationAttributes) at System.Activator.CreateInstance(Type type,
BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo
culture, Object[] activationAttributes) at
Common.Logging.LogManager.BuildLoggerFactoryAdapter()
--- End of inner exception stack trace --- at Common.Logging.LogManager.BuildLoggerFactoryAdapter() at
Common.Logging.LogManager.get_Adapter() at
Spring.Testing.Microsoft.AbstractSpringContextTests..ctor() at
Spring.Testing.Microsoft.AbstractTransactionalDbProviderSpringContextTests..ctor()
at
OurCompany.Tests.Common.Domain.SATransactionalIntegrationTestsBase..ctor()
in SATransactionalIntegrationTestsBase.cs: line 19 at
OurCompany.DataAccess.DocumentManagement.Tests.EmailVerificationTokensAdapterTests.DeleteTests..ctor()
in DeleteTests.cs: line 20
Here is what we have in the config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.TraceLoggerFactoryAdapter, Common.Logging">
<arg key="level" value="All" />
<arg key="showLogName" value="true" />
<arg key="showDataTime" value="true" />
<arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
</factoryAdapter>
</logging>
</common>
</configuration>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是
web.config
文件的片段,步骤如下:您可以在
appSettings
中添加一个键,该键指向包含所有 Log4net 设置的外部配置文件:这是外部配置文件:
在代码中,您将设置
XmlConfigurator
:Here's a snippet of the
web.config
file, here's the steps:You can add a key in the
appSettings
that points to an external config file containing all the Log4net settings:Here's the external config file:
While in your code you will setup the
XmlConfigurator
: