【问题标题】:Common.Logging without app.config没有 app.config 的 Common.Logging
【发布时间】:2013-09-23 03:03:01
【问题描述】:

我正在尝试使用 Common.Logging 但我们不使用 app.config,我们的参数在几个 xml 配置文件或数据库中。

我如何告诉 Common.Logging 从其他文件中获取工厂适配器?

<common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
        <arg key="configType" value="INLINE" />
      </factoryAdapter>
    </logging>
  </common>

我需要从其他文件中获取此配置部分。

有什么建议吗?

最好的毕业生 卡洪

【问题讨论】:

    标签: .net vb.net logging config common.logging


    【解决方案1】:

    您没有指定 Common.Logging 的版本,但我假设并基于 2.1。

    我认为您可以通过实现 Common.Logging.IConfigurationReader 来做到这一点,类似于以下内容:

    Imports System.Xml
    
    Public Class ConfigReader
        Implements Common.Logging.IConfigurationReader
    
        Private _configPath As String
    
        Public Sub New(configPath As String)
            _configPath = configPath
        End Sub
    
        Public Function GetSection(sectionName As String) As Object Implements Common.Logging.IConfigurationReader.GetSection
            Dim xmlDoc As New XmlDocument()
            xmlDoc.Load(_configPath)
    
            Return (New Common.Logging.ConfigurationSectionHandler()).Create(Nothing, Nothing, xmlDoc.SelectSingleNode("configuration/" & sectionName))
        End Function
    End Class
    

    然后在应用程序启动时您可以调用

    Common.Logging.LogManager.Reset(New ConfigReader(pathToConfigFile))
    

    当然,这只有在您拥有一个包含所有参数的配置文件并且它使用与标准 Common.Logging 内容相同的格式时才有效。否则,您可能需要根据配置参数的来源进行大量手动配置文件解析或数据库调用。

    【讨论】:

      猜你喜欢
      • 2018-02-16
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      相关资源
      最近更新 更多