1

On stackoverflow are a couple of questions regarding how the get app.config values from code or using windows path variables.

  • But I would like to know if i can use an already defined key inside the app.config xml file?
  • The purpose is to avoid entereing the connection string many times.

Below you can see my connection string 'con_str':

<appSettings>
  <add key="con_str" value="myDatabaseConnectionString"/>
</appSettings>
....
<log4net>
    <appender name="AdoNetExceptionAppender" 
         type="log4net.Appender.AdoNetAppender">
      <bufferSize value="1" />
      <threshold value="FATAL"/>
      <connectionType ... />
      <connectionString 
                 value= .... <--- How to use value from 'con_str' above?
       />
      <commandText ... />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
    </appender>
</log4net>

Is there a way to read the value of the key con_str from the appSettingsnode within app.config?

Update 1 after comment from user chadiusvt

I changed my app.config like this:

<appSettings>
    <add key="con_strOrig" value="foo" />   
</appSettings>
<connectionStrings>
    <add name="connStr" connectionString="foo" />   
</connectionStrings>
    ...
<log4net>
  <appender name="AdoNetExceptionAppender" 
            type="log4net.Appender.AdoNetAppender">
  <bufferSize value="1" />
  <threshold value="FATAL"/>
  <connectionType ... />
  <connectionString value="connStr"  />
  ...

As you can see with the last update it is possible to reference the connectionStrings. But since in my code i always use appSettings i would have to change my code in many places. Is there a way to let the <appSettings> point to the connStr key from the node <connectionsStrings>?

Update 2 to adress the answer

I believe you misunderstood. I am looking for a way to reference another var inside the xml-file.

Community
  • 1
  • 1
surfmuggle
  • 5,527
  • 7
  • 48
  • 77
  • 1
    Maybe [this article](http://stackoverflow.com/questions/2441359/can-you-pull-the-connectionstring-for-a-log4net-adonetappender-from-elsewhere-in) would be of help? It doesn't specify the connectionstring by name, but if you only have one, this might be sufficient. – Soturi Sep 09 '13 at 13:55

2 Answers2

3

Simply use this line :

System.Configuration.ConfigurationManager.AppSettings["con_str"]
Franck
  • 4,438
  • 1
  • 28
  • 55
  • I believe you misunderstood. I would like to reference the value from the connectionstring from inside the app.config xml-file. The following did not work: `` – surfmuggle Sep 09 '13 at 18:53
  • you can use the line above to READ the settings then by code set the log 4 net equals to that one. so you only have 1 to manage. – Franck Sep 09 '13 at 20:02
  • yes like in program.cs when you start the application you can override these settings. just read the "con_str" and set the log4net one. – Franck Sep 10 '13 at 17:18
1

As of my knowledge it's not possible to assign the section name with values.

I've some alternative solution to accomplish your task.

Assign name in to value dynamically in Program.CS

string connectionString = ConfigurationManager
  .ConnectionStrings["connStr"].ConnectionString;
ConfigurationManager.AppSettings
  .Set("con_strOrig", connectionString);
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
Bala
  • 618
  • 5
  • 21