【问题标题】:How to point Log4net to its configuration file?如何将 Log4net 指向其配置文件?
【发布时间】:2012-01-31 15:41:18
【问题描述】:

我们最近将所有测试从 NUnit 移到了 MsTest。我们将 Spring.NET 用于 IOC 容器和 Log4Net。尽管在测试项目中有 log4net.xml,但 Log4Net 无法找到它。有什么想法吗?

这是我们收到的错误:

无法创建类的实例 OurCompany.DataAccess.DocumentManagement.Tests.EmailVerificationTokensAdapterTests.DeleteTests。 错误:Common.Logging.ConfigurationException:无法创建 Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter 类型的实例。 可能的解释是缺少零 arg 和单个 arg NameValueCollection 构造函数---> System.Reflection.TargetInvocationException:已引发异常 通过调用的目标。 ---> Common.Logging.ConfigurationException:log4net 配置文件 “log4net.xml”不存在。在 Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter..ctor(NameValueCollection 特性) --- 内部异常堆栈跟踪结束 --- 在 System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo 方法、Object[] 参数、参考 SignatureStruct 签名、RuntimeType 声明类型)在 System.RuntimeMethodHandle.InvokeConstructor(IRuntimeMethodInfo 方法、Object[] 参数、SignatureStruct 签名、RuntimeType 声明类型)在 System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr、Binder binder、Object[] 参数、CultureInfo 文化) 在 System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder 活页夹、Object[] 参数、CultureInfo 文化、Object[] 激活属性)在 System.Activator.CreateInstance(类型类型, BindingFlags bindingAttr、Binder binder、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>

【问题讨论】:

    标签: log4net log4net-configuration


    【解决方案1】:

    这是web.config 文件的sn-p,步骤如下: 您可以在 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")))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      相关资源
      最近更新 更多