2012-01-31 21 views
1

我們最近將所有的測試從NUnit移到了MsTest。我們使用Spring.NET作爲IOC容器和Log4Net。儘管在測試項目中,Log4Net仍然無法找到log4net.xml。任何想法可能是什麼?如何將Log4net指向其配置文件?

這是我們收到的錯誤:

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

以下是我們在配置文件中:

<?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> 

回答

1

這裏是web.config文件的一個片段,這裏的步驟: 您可以在appSettings中添加一個密鑰,該密鑰指向包含所有Log4net設置的外部配置文件:

<appSettings> 
     <add key="log4net.Config" value="Configs\develop.config" /> 
    </appSettings> 

這裏的外部配置文件:

<log4net> 
    <root> 
     <appender-ref ref="LogFileAppender" /> 
    </root> 
    ... 
</log4net> 

雖然在代碼中您將建立XmlConfigurator

Config.XmlConfigurator.Configure(New FileInfo(ConfigurationManager.AppSettings("log4net.Config"))) 
相關問題