2013-08-16 44 views
0

我已閱讀在CodeProject話題很好的介紹,在這裏發現了一個有趣的答案在堆棧* 溢出 *,但在重建我讀失敗似乎是我自己的嘗試。我想我錯過了一些東西,但找不到什麼。如何正確嵌套config-sections config-elements?

以下異常讓我覺得,也許是我怎麼能窩配置截面/ -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

我花了一會兒才能找到問題。看來這些集合不知道在配置文件中尋找哪些元素。因此,添加在收藏ConfigurationCollection屬性的ITEMNAME將這樣的伎倆:

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

的ITEMNAME必須設置爲在集合元素的配置文件中使用的標記名,並通過「AddItemName =」傳遞.. 「'