使用配置文件部分覆盖/合并标准编程配置

时间:2012-09-09 05:53:24

标签: configuration nlog

我们遵循一套标准的约定来记录各种应用程序,将这种配置打包为通过代码配置并包含在一个通用程序集中是明智的。

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

这可以用NLog开箱即用吗?

1 个答案:

答案 0 :(得分:0)

我知道这不是 by-code (我不知道该怎么做),但你可以使用<include>和XML配置来覆盖变量之类的东西。以下是使用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>