【问题标题】:Use external log4net configuration file with environment variables使用带有环境变量的外部 log4net 配置文件
【发布时间】:2016-04-04 17:15:54
【问题描述】:

我正在尝试将我的应用程序的所有配置和日志文件分组到一个基本文件夹 (C:\MY_PRODUCT\ ) 以具有以下结构:

C:\MY_PRODUCT\CONFIGURATION\
C:\MY_PRODUCT\LOGS\
...

我正在使用 log4net 进行日志记录,并且在 log4net FileAppender 配置中使用环境变量可以正常工作:

<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file value="${PRODUCT_BASE}\LOGS\file.log" />
<threshold value="ALL" />
<appendToFile value="true" />
<maximumFileSize value="10MB" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="200" />
<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="[%date{O}] [%-5level] [%-3thread] [%logger] [%method] %message%newline" />
</layout>

但是,通过在新文件(C:\MY_PRODUCT\CONFIGURATION\Log4Net.config)中分离 log4net 配置,通过设置属性 ConfigSource 使用环境变量时 log4net 无法找到该配置文件强>:

<log4net configSource="${PRODUCT_BASE}\CONFIGURATION\Log4Net.config" />

请注意,如果我将 ${PRODUCT_BASE} 环境变量替换为其值(路径 C:\MY_PRODUCT),它可以正常工作,并且 log4net 能够找到配置文件。

Log4Net 日志:

我启用了 log4net 调试模式以查看发生了什么,我收到以下错误:

log4net:ERROR 无法解析配置文件。是否指定为: System.Configuration.ConfigurationErrorsException:无法打开 configSource 文件“${PRODUCT_BASE}\config\Log4Net.config”。 (Tests\bin\Debug\MY_PRODUCT.Core.Tests.dll.config 第 10 行) 在 System.Configuration.BaseConfigurationRecord.EvaluateOne(字符串 [] 键,SectionInput 输入,布尔 isTrusted,FactoryRecord factoryRecord,SectionRecord sectionRecord,对象 parentResult) 在 System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord,SectionRecord sectionRecord,对象 parentResult,布尔 getLkg,布尔 getRuntimeObject,对象和结果,对象和 resultRuntimeObject) 在 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串 configKey,布尔 getLkg,布尔 checkPermission,布尔 getRuntimeObject,布尔 requestIsHere,对象和结果,对象和 resultRuntimeObject) 在 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串 configKey,布尔 getLkg,布尔 checkPermission,布尔 getRuntimeObject,布尔 requestIsHere,对象和结果,对象和 resultRuntimeObject) 在 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串 configKey,布尔 getLkg,布尔 checkPermission,布尔 getRuntimeObject,布尔 requestIsHere,对象和结果,对象和 resultRuntimeObject) 在 System.Configuration.BaseConfigurationRecord.GetSection(字符串 configKey) 在 System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(字符串 sectionName) 在 System.Configuration.ConfigurationManager.GetSection(字符串 sectionName) 在 log4net.Config.XmlConfigurator.InternalConfigure(ILoggerRepository 存储库)

我不知道Log4Net是否扩展了ConfigSource属性的环境变量,我已经在Apache Log4Net问题平台上发布了问题,直到他们回答或解决问题,有没有办法实现这种方法?

【问题讨论】:

    标签: c# .net log4net


    【解决方案1】:

    谷歌搜索后,似乎不是Log4Net问题,正如他们在msdn post上所说:

    不,这不能在本地完成。

    最后我在 app.config 中添加了一个新的应用程序设置:

    <add key="log4net.config" value="%PRODUCT_BASE%\config\Log4Net.config"/>
    

    以及在启动时配置 log4net 时:

    string log4NetConfigFile = ConfigurationManager.AppSettings["log4net.config"];
            var fileInfo = new FileInfo(Environment.ExpandEnvironmentVariables(log4NetConfigFile));
            XmlConfigurator.Configure(fileInfo);
    

    我希望这能帮助其他遇到同样问题的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-31
      • 2017-10-10
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多