如何正确嵌套config-sections config-elements?

时间:2013-08-16 09:49:33

标签: c# web-config configurationmanager

我已经阅读了有关CodeProject主题的精彩介绍,并在stack * overflow *中找到了一个有趣的答案,但似乎我自己尝试重新创建我读取的内容失败了。我想我错过了一些但却找不到的东西。

以下异常让我想到,可能存在一些限制,我如何嵌套我没有正确遵守的配置节/ -elements / -elementcollections。

如果有人能解释如何正确地做到这一点(或指向彻底且易于阅读的解释),甚至可能指出我在我的案例中做错了什么,那将是可爱的!

提前多多谢谢!

我得到的例外是:

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

Parser Error Message: Unrecognized element 'LoggerRegistration'.

Source Error: 

Line 29:     <LoggerRegistrations>
Line 30:         
Line 31:       <LoggerRegistration Name="Fachlich">
Line 32:         <LogMappers>
Line 33:           <LogMapper  Name="Standard"

Source File: C:\P\INT_TRWeb\DEV\ITERGO.Tarifrechner\ITERGO.TR.WebUi\web.config    Line: 31 

来自我的web.config的相应摘录:

<configuration>
    <configSections>
        <section    Name="LoggerSettings" 
                    Type="LoggingTest.Core.LoggingSettings, LoggingTest.Core, Version=1.0.0.0, Culture=neutral"/>
    </configSections>

    <LoggerSettings    
        ServerId = "..."
        ServerName = "..."
        StageId = "..."
        WebId = "..."
        MandantId = "..."
        PartnerId = "..."
        ExternalPartnerId = "..."
        AppName = "...">

        <LoggerRegistrations>

            <LoggerRegistration Name="Fachlich">
                <LogMappers>
                    <LogMapper  Name="Standard"
                                MapperType="Logging.Core.LogMapperImpl.TrLogMapper"
                                MapperSubType="Logging.Entity.StandardLogEntry"
                                LogLevel="Information" />
                    <LogMapper  Name="Statistik"
                                MapperType="Logging.Core.LogMapperImpl.TrLogMapper"
                                MapperSubType="Logging.Entity.StatisticalLogEntry"
                                LogLevel="Information" />
                 </LogMappers>
            </LoggerRegistration>

            <LoggerRegistration Name="Technisch">
                <LogMappers>
                    <LogMapper  Name="Standard"
                                MapperType="Logging.Core.LogMapperImpl.TrLogMapper"
                                MapperSubType="Logging.Entity.StandardLogEntry"
                                LogLevel="Warning" />
                    <LogMapper  Name="Statistik"
                                MapperType="Logging.Core.LogMapperImpl.TrLogMapper"
                                MapperSubType="Logging.Entity.StatisticalLogEntry"
                                LogLevel="Information" />
                    <LogMapper  Name="EntLib"
                                MapperType="Logging.Core.LogMapperImpl.EntLibMapper"
                                MapperSubType="none" />
                </LogMappers>
            </LoggerRegistration>

        </LoggerRegistrations>
    </LoggerSettings>
</configuration>

以下是相应的类:

public class LoggerSettings : ConfigurationSection
{

    [ConfigurationProperty("ServerId", DefaultValue = "DevLocalhost")]
    public string ServerId 
    { 
        get { return (string) this["ServerId"]; }
        set { this["ServerId"] = value; }
    }

    [ConfigurationProperty("ServerName", DefaultValue = null)]
    public string ServerName
    { 
        get { return (string) this["ServerName"] ?? System.Environment.MachineName; }
        set { this["ServerName"] = value ?? System.Environment.MachineName; }
    }

    [ConfigurationProperty("StageId", DefaultValue = "DevLocal")]
    public string StageId
    { 
        get { return (string) this["StageId"]; }
        set { this["StageId"] = value; }
    }

    [ConfigurationProperty("WebId", DefaultValue = null)]
    public string WebId
    { 
        get { return (string) this["WebId"] ?? System.Environment.MachineName; }
        set { this["WebId"] = value ?? System.Environment.MachineName; }
    }

    [ConfigurationProperty("MandantId", DefaultValue = "n/a")]
    public string MandantId
    { 
        get { return (string) this["MandantId"]; }
        set { this["MandantId"] = value; }
    }

    [ConfigurationProperty("PartnerId", DefaultValue = "n/a")]
    public string PartnerId
    { 
        get { return (string) this["PartnerId"]; }
        set { this["PartnerId"] = value; }
    }

    [ConfigurationProperty("ExternalPartnerId", DefaultValue = "n/a")]
    public string ExternalPartnerId
    { 
        get { return (string) this["ExternalPartnerId"]; }
        set { this["ExternalPartnerId"] = value; }
    }

    [ConfigurationProperty("AppName", DefaultValue = "n/a")]
    public string AppName
    { 
        get { return (string) this["AppName"]; }
        set { this["AppName"] = value; }
    }

    [ConfigurationProperty("LoggerRegistrations")]
    public LoggerRegistrationCollection LoggerRegistrations
    {
        get { return (LoggerRegistrationCollection)this["LoggerRegistrations"]; }
    }

}

[ConfigurationCollection(typeof(LoggerRegistration))]
public class LoggerRegistrationCollection : ConfigurationElementCollection
{
    public new LoggerRegistration this[string Name]
    {
        get { return (LoggerRegistration)base.BaseGet(Name); }
    }

    public LoggerRegistration this[int index]
    {
        get { return (LoggerRegistration)base.BaseGet(index); }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new LoggerRegistration();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((LoggerRegistration)element).Name;
    }
}

public class LoggerRegistration : ConfigurationElement
{
    [ConfigurationProperty("Name", IsRequired=true)]
    public string Name
    {
        get { return (string)base["Name"]; }
        set { base["Name"] = value; }
    }

    [ConfigurationProperty("LogMappers")]
    public LogMapperElementCollection LogMappers
    {
        get { return (LogMapperElementCollection) this["LogMappers"]; }
    }

}


[ConfigurationCollection(typeof(LogMapperElement))]
public class LogMapperElementCollection : ConfigurationElementCollection
{
    public new LogMapperElement this[string Name]
    {
        get { return (LogMapperElement)base.BaseGet(Name); }
    }

    public LogMapperElement this[int index]
    {
        get { return (LogMapperElement)base.BaseGet(index); }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new LogMapperElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((LogMapperElement)element).Name;
    }
}

public class LogMapperElement : ConfigurationElement
{
    [ConfigurationProperty("Name", IsRequired = true)]
    public string Name
    {
        get { return (string)base["Name"]; }
        set { base["Name"] = value; }
    }

    [ConfigurationProperty("MapperType", IsRequired = true)]
    public Type MapperType
    {
        get { return Type.GetType((string) base["MapperType"]); }
        set { base["MapperType"] = value.FullName; }
    }

    [ConfigurationProperty("MapperSubType", DefaultValue = null)]
    public Type MapperSubType
    {
        get { return (string) base["MapperSubType"] == "none" ? null : Type.GetType((string) base["MapperSubType"]); }
        set { base["MapperSubType"] = value != null ? value.FullName : "none"; }
    }

    [ConfigurationProperty("LogLevel", DefaultValue = LogLevel.Warning)] 
    public LogLevel LogLevel
    {
        get { return (LogLevel) Enum.Parse(typeof (LogLevel), (string) this["LogLevel"]); }
        set { this["LogLevel"] = value.ToString(); }
    }
}

1 个答案:

答案 0 :(得分:1)

我花了一些时间才发现问题。似乎集合不知道配置文件中要查找哪些元素。因此,在Collections ConfigurationCollection-Attribute中添加ItemName将起到作用:

...
[ConfigurationCollection(typeof(LoggerRegistration), AddItemName = "LoggerRegistration")]
public class LoggerRegistrations : ConfigurationElementCollection
{
...
.
...
[ConfigurationCollection(typeof(LogMapperElement), AddItemName = "LogMapper")]
public class LogMappers : ConfigurationElementCollection
...

ItemName必须设置为集合元素的配置文件中使用的标记名,并通过'AddItemName =“...”'

传递