我需要在运行时修改
当我这样做时,
我也无法将其强制转换为
我必须回归使用普通的 XML 来完成这个任务吗?我真的不想重新发明轮子。
app.config
中的<configuration><system.diagnostics>
部分,以便我可以:
在
<sharedListeners>
元素下添加CustomTraceListener
,这需要特殊的initializeData
,只能在运行时确定。将
CustomTraceListener
共享侦听器添加到现有源的<source><listeners>
元素下。将
CustomTraceListener
持久化到其他装载其跟踪源和侦听器配置的程序集中的配置文件中。
app.config
中相关部分目前看起来是这样的:<system.diagnostics>
<sources>
<source name="mysource" switchName="..." switchType="...">
<listeners>
<add name="console" />
<add name="customtracelistener" /> /// need to add new listener here
</listeners>
</source>
</sources>
<sharedListeners>
<add name="console" type="..." initializeData="..." />
<add name="customtracelistener" type="..." initializeData="..."> /// need to add custom trace listener here
<filter type="..." initializeData="Warning"/> /// with a filter
</add>
</sharedListeners>
<switches>
<add name="..." value="..." />
</switches>
</system.diagnostics>
使用 ConfigurationManager
我可以轻松地执行以下操作:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection diagnostics = config.GetSection("system.diagnostics");
当我这样做时,
diagnostics
是一个 System.Diagnostics.SystemDiagnosticsSection
类型。有趣的是,我无法将 diagnostics
强制转换为 SystemDiagnosticsSection
类型,因为我找不到它在哪个命名空间中。无论如何,ConfigurationSection
似乎没有任何方法可用于将数据写入该部分。我也无法将其强制转换为
NameValueConfigurationCollection
,因为 diagnostics
基类型是 ConfigurationSection
。我听说过这种技术,但似乎我无法使用它。我必须回归使用普通的 XML 来完成这个任务吗?我真的不想重新发明轮子。