Sam

Sam

  • NA
  • 1
  • 0

App.Config help!!!

Dec 27 2008 1:42 AM

Hi,

 

I have a logging class that I use in all my classes.  The code for the logger was originally used like so:

    Properties.Settings s = new Properties.Settings();

    String path = s.logPath;

the app.config file looks likes this: (also every project that uses the Logger must have this AppSettingsSection in the  App.config)

 

<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="G2G.Logging.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </sectionGroup>
    </configSections>
    <applicationSettings>
        <G2G.Logging.Properties.Settings>
  <setting name="logPath" serializeAs="String">
                <value />
            </setting>
            <setting name="logFilePrefix" serializeAs="String">
                <value />
            </setting>
            <setting name="logInUTC" serializeAs="String">
                <value>True</value>
            </setting>
            <setting name="loggingEnabled" serializeAs="String">
                <value>True</value>
            </setting>
        </G2G.Logging.Properties.Settings>
    </applicationSettings>
</configuration>

however this caused a problem in the following situation.  A class that usses the Logger class is also being used by a COM object.  For some reason, the COM object uses some funky .Config file.  To remedy this I changed the way I retrieve the app settings as follows:

    Configuration cs = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ClientSettingsSection settingsSection = ((ClientSettingsSection)(cs.SectionGroups["applicationSettings"].Sections["G2G.Logging.Properties.Settings"]));
    SettingElementCollection settings = settingsSection.Settings;

    String path = settings.Get("logPath").Value.ValueXml.InnerText;

This works beautifully but it is very ugly in my opinion.  Does anyone have any suggestions of a more pretty way to do this?


Answers (1)