如何在app.config中重命名部分?部分名称不起作用

时间:2010-12-27 21:11:41

标签: c# .net app-config configurationsection

是不是可以在app.config中重命名标签?

如果我使用以下

...
<sectionGroup name="common">
  <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
...
<common>
  <logging>
    <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
      <arg key="configType" value="INLINE" />
    </factoryAdapter>
  </logging>
</common>
...

一切都按预期工作(使用Common Logging进行日志记录已完成)。 但是,如果我更改节名称,则忽略配置,例如如果我将该组重命名为common 到mycommon。

<sectionGroup name="mycommon">
  <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
...
<mycommon>
  <logging>
    <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
      <arg key="configType" value="INLINE" />
    </factoryAdapter>
  </logging>
</mycommon>
...

有人知道这个伎俩吗?

2 个答案:

答案 0 :(得分:1)

也许XML路径已连接到log4net,请尝试询问它们。

答案 1 :(得分:1)

解决方案在代码本身中找到......

public static class LogManager
{
    /// <summary>
    /// The name of the default configuration section to read settings from.
    /// </summary>
    /// <remarks>
    /// You can always change the source of your configuration settings by setting another <see cref="IConfigurationReader"/> instance
    /// on <see cref="ConfigurationReader"/>.
    /// </remarks>
    public static readonly string COMMON_LOGGING_SECTION = "common/logging";

    private static IConfigurationReader _configurationReader;

所以你可以创建一个新类实现IConfigurationReader接口,扩展/替换LogManager(或替换字符串,但这需要重新编译Common.Logging)。

感谢Common.Logging的评论良好的代码......我在调试后得到了它。

相关问题