G
Guest
H
I've created my own custom configuration section handler class to interpret custom sections in my app config file. However I keep on getting a ConfigurationException when trying to access the section from code as in
Loftware.Diagnostics.CustomListeners listeners = (Loftware.Diagnostics.CustomListeners)ConfigurationSettings.GetConfig("customListeners")
The definition of the section handler in the app config file is
<configuration><configSections><section name="customListeners" type="Loftware.Diagnostics.CustomListenersHandler,Loftware.Diagnostics" /></configSections
The definition of the custom section within the app config file is
<customListeners><listener name="LogFile" type="Loftware.Diagnostics.FileTraceListener"><parameters><fileName>Loftware.Diagnostics.Test.xml</fileName></parameters><decorator type="Loftware.Diagnostics.XMLTraceDecorator"><parameters><attributeXml>true</attributeXml></parameters></decorator><displayStrategy type="Loftware.Diagnostics.DefaultDisplayStrategy" value="7" /></listener></customListeners
The problem seems to be with the format of the <customListeners> node in the app config file. If I change the <customListeners> to be a single line statement without any line feeds or tabs in the app config file as in following everything works fine
<customListeners><listener name="LogFile" type="Loftware.Diagnostics.FileTraceListener"><parameters><fileName>Loftware.Diagnostics.Test.xml</fileName></parameters><decorator type="Loftware.Diagnostics.XMLTraceDecorator"><parameters><attributeXml>true</attributeXml></parameters></decorator><displayStrategy type="Loftware.Diagnostics.DefaultDisplayStrategy" value="7" /></listener></customListeners
Why do I need to remove the line feeds and tabs to get it working? The complete exception dump is
System.Configuration.ConfigurationException: Exception in configuration section handler (C:\Development\VS .NET\C#\Loftware\Diagnostics\Test\bin\Debug\Loftware.Diagnostics.Test.exe.config line 102) ---> System.NullReferenceException: Object reference not set to an instance of an object
at Loftware.Diagnostics.CustomListeners..ctor(XmlNode section) in c:\development\vs .net\c#\loftware\diagnostics\customlisteners.cs:line 15
at Loftware.Diagnostics.CustomListenersHandler.Create(Object parent, Object configContext, XmlNode section) in c:\development\vs .net\c#\loftware\diagnostics\customlisteners.cs:line 1
at System.Configuration.ConfigurationRecord.EvaluateRecursive(IConfigurationSectionHandler factory, Object config, String[] keys, Int32 iKey, XmlTextReader reader
--- End of inner exception stack trace --
at System.Configuration.ConfigurationRecord.EvaluateRecursive(IConfigurationSectionHandler factory, Object config, String[] keys, Int32 iKey, XmlTextReader reader
at System.Configuration.ConfigurationRecord.Evaluate(String configKey
at System.Configuration.ConfigurationRecord.ResolveConfig(String configKey
at System.Configuration.ConfigurationRecord.GetConfig(String configKey
at System.Configuration.DefaultConfigurationSystem.System.Configuration.IConfigurationSystem.GetConfig(String configKey
at System.Configuration.ConfigurationSettings.GetConfig(String sectionName
at Loftware.Diagnostics.Test.Test..cctor() in c:\development\vs .net\c#\loftware\diagnostics\test\test.cs:line 3
Thank
Carel
I've created my own custom configuration section handler class to interpret custom sections in my app config file. However I keep on getting a ConfigurationException when trying to access the section from code as in
Loftware.Diagnostics.CustomListeners listeners = (Loftware.Diagnostics.CustomListeners)ConfigurationSettings.GetConfig("customListeners")
The definition of the section handler in the app config file is
<configuration><configSections><section name="customListeners" type="Loftware.Diagnostics.CustomListenersHandler,Loftware.Diagnostics" /></configSections
The definition of the custom section within the app config file is
<customListeners><listener name="LogFile" type="Loftware.Diagnostics.FileTraceListener"><parameters><fileName>Loftware.Diagnostics.Test.xml</fileName></parameters><decorator type="Loftware.Diagnostics.XMLTraceDecorator"><parameters><attributeXml>true</attributeXml></parameters></decorator><displayStrategy type="Loftware.Diagnostics.DefaultDisplayStrategy" value="7" /></listener></customListeners
The problem seems to be with the format of the <customListeners> node in the app config file. If I change the <customListeners> to be a single line statement without any line feeds or tabs in the app config file as in following everything works fine
<customListeners><listener name="LogFile" type="Loftware.Diagnostics.FileTraceListener"><parameters><fileName>Loftware.Diagnostics.Test.xml</fileName></parameters><decorator type="Loftware.Diagnostics.XMLTraceDecorator"><parameters><attributeXml>true</attributeXml></parameters></decorator><displayStrategy type="Loftware.Diagnostics.DefaultDisplayStrategy" value="7" /></listener></customListeners
Why do I need to remove the line feeds and tabs to get it working? The complete exception dump is
System.Configuration.ConfigurationException: Exception in configuration section handler (C:\Development\VS .NET\C#\Loftware\Diagnostics\Test\bin\Debug\Loftware.Diagnostics.Test.exe.config line 102) ---> System.NullReferenceException: Object reference not set to an instance of an object
at Loftware.Diagnostics.CustomListeners..ctor(XmlNode section) in c:\development\vs .net\c#\loftware\diagnostics\customlisteners.cs:line 15
at Loftware.Diagnostics.CustomListenersHandler.Create(Object parent, Object configContext, XmlNode section) in c:\development\vs .net\c#\loftware\diagnostics\customlisteners.cs:line 1
at System.Configuration.ConfigurationRecord.EvaluateRecursive(IConfigurationSectionHandler factory, Object config, String[] keys, Int32 iKey, XmlTextReader reader
--- End of inner exception stack trace --
at System.Configuration.ConfigurationRecord.EvaluateRecursive(IConfigurationSectionHandler factory, Object config, String[] keys, Int32 iKey, XmlTextReader reader
at System.Configuration.ConfigurationRecord.Evaluate(String configKey
at System.Configuration.ConfigurationRecord.ResolveConfig(String configKey
at System.Configuration.ConfigurationRecord.GetConfig(String configKey
at System.Configuration.DefaultConfigurationSystem.System.Configuration.IConfigurationSystem.GetConfig(String configKey
at System.Configuration.ConfigurationSettings.GetConfig(String sectionName
at Loftware.Diagnostics.Test.Test..cctor() in c:\development\vs .net\c#\loftware\diagnostics\test\test.cs:line 3
Thank
Carel