如何将Log4net指向其配置文件?

发布于 2025-01-01 21:09:15 字数 2798 浏览 4 评论 0原文

我们最近将所有测试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放血 2025-01-08 21:09:15

这是 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")))

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:

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

Here's the external config file:

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

While in your code you will setup the XmlConfigurator:

Config.XmlConfigurator.Configure(New FileInfo(ConfigurationManager.AppSettings("log4net.Config")))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文