c# - how to properly nest config-sections config-elements? -


i have read nice introduction on topic on codeproject , found interesting answer here @ stack*overflow*, seems own attempts @ recreating read fails. guess missed can't find out what.

the following exception makes me think, maybe there restrictions in how can nest configuration-sections/-elements/-elementcollections have not obeyed.

would lovely if explain how (or point thorough , easy read explanations) , maybe point out did wrong in case!

thanks lot in advance!

the exception is:

description: error occurred during processing of configuration file required service request. please review specific error details below , modify 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  

the corresponding excerpt 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> 

and here corresponding classes:

public class loggersettings : configurationsection {      [configurationproperty("serverid", defaultvalue = "devlocalhost")]     public string serverid      {          { return (string) this["serverid"]; }         set { this["serverid"] = value; }     }      [configurationproperty("servername", defaultvalue = null)]     public string servername     {          { return (string) this["servername"] ?? system.environment.machinename; }         set { this["servername"] = value ?? system.environment.machinename; }     }      [configurationproperty("stageid", defaultvalue = "devlocal")]     public string stageid     {          { return (string) this["stageid"]; }         set { this["stageid"] = value; }     }      [configurationproperty("webid", defaultvalue = null)]     public string webid     {          { return (string) this["webid"] ?? system.environment.machinename; }         set { this["webid"] = value ?? system.environment.machinename; }     }      [configurationproperty("mandantid", defaultvalue = "n/a")]     public string mandantid     {          { return (string) this["mandantid"]; }         set { this["mandantid"] = value; }     }      [configurationproperty("partnerid", defaultvalue = "n/a")]     public string partnerid     {          { return (string) this["partnerid"]; }         set { this["partnerid"] = value; }     }      [configurationproperty("externalpartnerid", defaultvalue = "n/a")]     public string externalpartnerid     {          { return (string) this["externalpartnerid"]; }         set { this["externalpartnerid"] = value; }     }      [configurationproperty("appname", defaultvalue = "n/a")]     public string appname     {          { return (string) this["appname"]; }         set { this["appname"] = value; }     }      [configurationproperty("loggerregistrations")]     public loggerregistrationcollection loggerregistrations     {         { return (loggerregistrationcollection)this["loggerregistrations"]; }     }  }  [configurationcollection(typeof(loggerregistration))] public class loggerregistrationcollection : configurationelementcollection {     public new loggerregistration this[string name]     {         { return (loggerregistration)base.baseget(name); }     }      public loggerregistration this[int index]     {         { 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     {         { return (string)base["name"]; }         set { base["name"] = value; }     }      [configurationproperty("logmappers")]     public logmapperelementcollection logmappers     {         { return (logmapperelementcollection) this["logmappers"]; }     }  }   [configurationcollection(typeof(logmapperelement))] public class logmapperelementcollection : configurationelementcollection {     public new logmapperelement this[string name]     {         { return (logmapperelement)base.baseget(name); }     }      public logmapperelement this[int index]     {         { 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     {         { return (string)base["name"]; }         set { base["name"] = value; }     }      [configurationproperty("mappertype", isrequired = true)]     public type mappertype     {         { return type.gettype((string) base["mappertype"]); }         set { base["mappertype"] = value.fullname; }     }      [configurationproperty("mappersubtype", defaultvalue = null)]     public type mappersubtype     {         { 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     {         { return (loglevel) enum.parse(typeof (loglevel), (string) this["loglevel"]); }         set { this["loglevel"] = value.tostring(); }     } } 

took me while find problem. seems collections don't know elementitems in config file. therefore adding itemname @ collections configurationcollection-attribute trick:

... [configurationcollection(typeof(loggerregistration), additemname = "loggerregistration")] public class loggerregistrations : configurationelementcollection { ... . ... [configurationcollection(typeof(logmapperelement), additemname = "logmapper")] public class logmappers : configurationelementcollection ... 

the itemname must set tagname used in config file collections elements, , passed via 'additemname="..."'


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -