我在我的项目中使用nlog。我的web.config看起来像这样:
<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>
两个相同的连接字符串!我的问题是如何只保留一个连接字符串?
答案 0 :(得分:17)
首先将add providerName属性添加到连接字符串。然后使用connectionStringName而不是connectionString,并从设置中引用连接字符串。
<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>
答案 1 :(得分:6)
应该可以使用目标元素的属性ConnectionStringName。
例如:
<targets>
<target name="database" type="Database" connectionStringName="SQL_ConnStr" commandText="INSERT INTO ...">
</target>
这将直接从connectionstrings部分访问连接字符串。