grayhawk
grayhawk

Reputation:

Using connection strings twice in web.config file; another for Nlog config

I am using nlog in my project. My web.config looks like this:

<connectionStrings>
  <add name="SQL_ConnStr" connectionString="Initial Catalog=ConfigDB;Provider=SQLOLEDB; Data Source=mysqlserver; User ID=sa; Password=sa; Persist Security Info=True;"/>
</connectionStrings>
...
<nlog>
<targets>
  <target name="database" type="Database" dbProvider="sqlserver" **connectstring="Initial Catalog=ConfigDB;Provider=SQLOLEDB; Data Source=mysqlserver; User ID=sa; Password=sa"** commandText="INSERT INTO ...">
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="database"/>
</rules>
</nlog>

Two identical connection strings! My question is how to keep only one connection string?

Upvotes: 8

Views: 5726

Answers (2)

Emond
Emond

Reputation: 50682

First add the add providerName attribute to connection string. Then use connectionStringName instead of connectionString and refer to the connection string from the settings.

<connectionStrings>
  <add name="SQL_ConnStr" 
       providerName="System.Data.SqlClient"
       connectionString="Initial Catalog=ConfigDB;Provider=SQLOLEDB; Data Source=mysqlserver; User ID=sa; Password=sa; Persist Security Info=True;"/>
</connectionStrings>
...
<nlog>
  <targets>
    <target name="database" 
            type="Database" 
            dbProvider="sqlserver"
            connectionStringName="SQL_ConnStr" 
            commandText="INSERT INTO ...">
    </target>
  </targets>
  <rules>
    <logger name="*" 
            minlevel="Debug" 
            writeTo="database"/>
  </rules>
</nlog>

Upvotes: 21

JPReddy
JPReddy

Reputation: 65523

It should be possible with property ConnectionStringName of target element.

For ex:

<targets>
  <target name="database" type="Database" connectionStringName="SQL_ConnStr" commandText="INSERT INTO ...">
</target>

This will access the connection string from connectionstrings section directly.

Upvotes: 7

Related Questions