【发布时间】:2012-05-05 21:12:50
【问题描述】:
我发现了一些与 app.config/web.config 相关的 sn-ps 信息,这些信息暗示了 BCL 组件的几乎无代码配置,直接通过 app.config 进行。但是,鉴于 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"));
}
}
}
【问题讨论】:
标签: c# web-config app-config