在App.config

时间:2016-04-28 23:56:30

标签: c# .net configuration app-config

我们正在使用Carbonator app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="carbonator" type="Crypton.Carbonator.Config.CarbonatorSection, Crypton.Carbonator"/>
  </configSections>

  <carbonator defaultCulture="en-US" logLevel="1" collectionInterval="1000" reportingInterval="1000" >
    <statsd server="127.0.0.1" port="8125" />
    <counters>
      <add path="processor_information.pct_processor_time.total" category="Processor" counter="% Processor Time" instance="_Total" />
      <add path="memory.available_MBytes" category="Memory" counter="Available MBytes" instance="" />
      <add path="memory.pct_commited_bytes_in_use" category="Memory" counter="% Committed Bytes In Use" instance="" />
    </counters>
  </carbonator>
</configuration>

我们希望允许用户在我们从<counters>元素引用的外部配置文件中配置自己的自定义计数器。例如,我们希望允许用户配置文件看起来像:

<add path="logical_disk.pct_free_space.C" category="LogicalDisk" counter="% Free Space" instance="C:" />
<add path="logical_disk.disk_read_bytes_per_sec.C" category="LogicalDisk" counter="Disk Read Bytes/sec" instance="C:" />
<add path="logical_disk.disk_write_bytes_per_sec.C" category="LogicalDisk" counter="Disk Write Bytes/sec" instance="C:" />

我甚至不知道这是否可能在appConfig元素之外,但任何帮助都表示赞赏。

1 个答案:

答案 0 :(得分:3)

根据this answer,它应该是可能的。同样的方式也在this article中描述。

但我认为这不是一个好主意 - 如果用户在配置扩展中出错,它将阻止应用程序执行,因为应用程序配置变得无效。

我宁愿使用app.config文件中的配置来提供默认值并自己实现一些用户配置。在这种情况下,您可以使用您喜欢的任何配置格式,例如JSON,这对用户来说也更好(更容易创建和编辑)。在您的应用程序中,您只需合并两个配置(app.config值是默认值,将被用户的配置覆盖)。