【问题标题】:Overriding/merging standard programmatic configuration with config file section使用配置文件部分覆盖/合并标准编程配置
【发布时间】:2012-09-09 05:53:24
【问题描述】:

我们有一套标准的约定,我们遵循这些约定来从我们的各种应用程序中进行日志记录,并且将此配置打包为按代码配置并包含在一个通用程序集中是明智的。

但是,有时我们需要覆盖此配置以更有效地调试生产问题。这可以通过让开发人员或管理员添加一个 NLog 配置部分来更轻松地完成,该部分可以读入并覆盖或添加到以编程方式完成的配置。

这可以用 NLog 开箱即用地完成吗?

【问题讨论】:

  • 您找到解决此问题的方法了吗?谢谢。

标签: configuration nlog


【解决方案1】:

我知道这不是 按代码(我不知道该怎么做),但是您可以使用带有 XML 配置的 <include> 来覆盖变量之类的东西。这是一个使用Web.config 的示例,它覆盖了其他项目的“标准”NLog.config

Web.config:

<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>

  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <include file="${basedir}\bin\NLog.config" />
    <variable name="fruit" value="Apples" />
  </nlog>
</configuration>

NLog.config(被复制到bin):

<?xml version="1.0"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <!-- Output Window -->
    <target name="debug" xsi:type="Debugger" layout="${fruit}|${level:uppercase=true}|${logger}|${message}"></target>
  </targets>
  <rules>
    <logger name="*" writeTo="debug" />
  </rules>
</nlog>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多