3

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

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

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

4

1 回答 1

0

我知道这不是代码(我不知道该怎么做),但是您可以使用<include>XML 配置来覆盖变量之类的东西。这是一个使用Web.config覆盖NLog.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>
于 2013-02-28T13:50:33.253 回答