如何在app.config文件中配置基类库?

时间:2021-10-05 07:29:23

I've found a couple of snippets of information pertaining to app.config/web.config that hints at almost codeless configuration of BCL components directly through the app.config. However, given the amount of tags suggested by the intellisense within the app.config, it suggests that there is a huge amount of possibilities for this that I can't find any useful information for.

我发现了一些与app.config / web.config相关的信息片段,它直接通过app.config暗示了几乎无代码的BCL组件配置。但是,考虑到app.config中intellisense建议的标签数量,它表明存在大量的可能性,我无法找到任何有用的信息。

Is there any documentation that supports this particular area of configuration files? I can find plenty of information on storing/retrieving configuration information and a small amount regarding writing custom configuration sections which I'm familiar with, but I cannot find any information regarding configuring BCL components this way. Does anyone have any reference material for this?

是否有任何文档支持配置文件的这个特定区域?我可以找到关于存储/检索配置信息的大量信息,以及关于编写我熟悉的自定义配置部分的少量信息,但是我找不到有关以这种方式配置BCL组件的任何信息。有没有人有这方面的参考资料?

One example I've come across is as follows:

我遇到的一个例子如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <trace autoflush="true" indentsize="2">
      <listeners>
        <add name="Console"
             type="System.Diagnostics.ConsoleTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
              traceOutputOptions="Timestamp" />
      </listeners>
    </trace>
    <switches>
      <add name="Logging.Program.Listener" value="Error" />
    </switches>
  </system.diagnostics>
</configuration>

Which may be consumed using code in a similar fashion to this:

可以使用与此类似的方式使用代码来使用它:

class Program
{
  private static TextWriterTraceListener tw = new TextWriterTraceListener();
  private static TraceSwitch ts = new TraceSwitch("Logging.Program.Listener", "Default Logging Level", "Off");

  static void Main(string[] args)
  {
    Trace.Listeners.Add(tw);

    try
    {
        throw (new EntryPointNotFoundException());
    }
    catch (EntryPointNotFoundException ex)
    {
        string TraceMessage = "Trace {0}: {1}";
        Trace.WriteLineIf(ts.TraceError, String.Format(TraceMessage, TraceLevel.Error, "Error Level Message"));
        Trace.WriteLineIf(ts.TraceWarning, String.Format(TraceMessage, TraceLevel.Warning, "Warning Level Message"));
        Trace.WriteLineIf(ts.TraceInfo, String.Format(TraceMessage, TraceLevel.Info, "Info Level Message"));
        Trace.WriteLineIf(ts.TraceVerbose, String.Format(TraceMessage, TraceLevel.Verbose, "Verbose Level Message"));
    }
  }
}

2 个解决方案

#1


One useful resource is the machine-level configuration files. The actual files are bare-bones, but there are ".comments" files alongside them that give fairly detailed examples of what can be achieved. For example, take a look in

一个有用的资源是机器级配置文件。实际的文件很简单,但是它们旁边还有“.comments”文件,它们提供了可以实现的相当详细的示例。例如,看看

C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config.comments

That will give you some idea of what's achievable. Anywhere where you see collection elements, as in the case of the <traceSwitches> and <traceListeners> elements, the individual <add> elements contained within may vary depending on what you are adding (i.e. the specific attributes on those <add> elements will vary depending on exactly what you're adding to the collection). For this, you'll need to consult specific areas of documentation, but searching for the <traceSwitches> element in MSDN ought to serve as a decent starting point there.

这会让你知道什么是可以实现的。在您看到集合元素的任何地方,如 元素的情况,其中包含的各个 元素可能会根据您添加的内容而有所不同(即这些 元素的特定属性将根据您添加到集合中的具体内容而有所不同。为此,您需要查阅特定的文档区域,但在MSDN中搜索 元素应该是一个不错的起点。

#2


They're all configurable this way. That's why you're not finding anything.

它们都是这样配置的。这就是你找不到任何东西的原因。

Ok, maybe not all, but certainly most. If you want to know, Use Reflector to find all the derived classes of System.configuration.ConfigurationSection, etc.

好吧,也许不是全部,但肯定是最多的。如果您想知道,请使用Reflector查找System.configuration.ConfigurationSection的所有派生类等。

#1


One useful resource is the machine-level configuration files. The actual files are bare-bones, but there are ".comments" files alongside them that give fairly detailed examples of what can be achieved. For example, take a look in

一个有用的资源是机器级配置文件。实际的文件很简单,但是它们旁边还有“.comments”文件,它们提供了可以实现的相当详细的示例。例如,看看

C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config.comments

That will give you some idea of what's achievable. Anywhere where you see collection elements, as in the case of the <traceSwitches> and <traceListeners> elements, the individual <add> elements contained within may vary depending on what you are adding (i.e. the specific attributes on those <add> elements will vary depending on exactly what you're adding to the collection). For this, you'll need to consult specific areas of documentation, but searching for the <traceSwitches> element in MSDN ought to serve as a decent starting point there.

这会让你知道什么是可以实现的。在您看到集合元素的任何地方,如 元素的情况,其中包含的各个 元素可能会根据您添加的内容而有所不同(即这些 元素的特定属性将根据您添加到集合中的具体内容而有所不同。为此,您需要查阅特定的文档区域,但在MSDN中搜索 元素应该是一个不错的起点。

#2


They're all configurable this way. That's why you're not finding anything.

它们都是这样配置的。这就是你找不到任何东西的原因。

Ok, maybe not all, but certainly most. If you want to know, Use Reflector to find all the derived classes of System.configuration.ConfigurationSection, etc.

好吧,也许不是全部,但肯定是最多的。如果您想知道,请使用Reflector查找System.configuration.ConfigurationSection的所有派生类等。