0

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

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

我遇到的一个例子如下:

<?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>

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

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"));
    }
  }
}
4

2 回答 2

2

一种有用的资源是机器级配置文件。实际文件是基本文件,但旁边有“.comments”文件,提供了可以实现的相当详细的示例。例如,看看

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

这将使您对可以实现的目标有所了解。任何你看到集合元素的地方,比如<traceSwitches>and<traceListeners>元素,其中包含的单个<add>元素可能会根据你添加的内容而有所不同(即,这些<add>元素的特定属性将根据你添加到集合中的确切内容而有所不同)。为此,您需要查阅特定领域的文档,但<traceSwitches>在 MSDN 中搜索该元素应该是一个不错的起点。

于 2009-04-14T22:10:35.203 回答
0

它们都可以通过这种方式进行配置。这就是为什么你没有找到任何东西。

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

于 2009-04-14T20:29:20.750 回答