2009-06-30 8 views
5

私は自分のプロジェクトでnlogを使用しています。私のweb.configは次のようになります:web.configファイルで接続文字列を2回使用する。別のNlog設定のため

<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> 

2つの同じ接続文字列!私の質問は、ただ1つの接続文字列を保持する方法ですか?

答えて

16

まず、connection stringにadd providerName属性を追加します。次に、connectionStringの代わりにconnectionStringNameを使用し、設定から接続文字列を参照します。

<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> 
+5

重要な情報を追加したいだけです。もちろん、connectionStringNameを使用する必要がありますが、接続文字列にproviderName属性を追加する必要があります(たとえば、msqlサーバーの場合はproviderName = "System.Data.SqlClient")。それがないと、NLogは "providerInvariantName 'パラメータの空でない文字列を期待しています。 –

+0

しかし、どこにconnectionStringsセクションを置くのですか? env.ConfigureNLog( "nlog.config"); – flexxxit

+0

@flexxxitのセクションで接続文字列名を見つけることができないというエラーが表示されます。これは別の質問です。 http://stackoverflow.com/questions/26882704/where-to-place-connection-string-in-web-configを参照してください。 –

6

ターゲット要素のConnectionStringNameプロパティで可能である必要があります。 EXのために

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

これは直接connectionStringsセクションからの接続文字列にアクセスします。

関連する問題