没有app.config的Common.Logging

时间:2013-09-18 11:16:14

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

我正在尝试使用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>

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

任何消化?

最好的regrads 卡洪

1 个答案:

答案 0 :(得分:2)

您没有指定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相同的格式时才会有效。否则,您可能需要进行一系列手动配置文件解析或数据库调用,具体取决于配置参数的来源。

相关问题