我在项目中使用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>
两个相同的连接字符串!我的问题是如何仅保留一个连接字符串?
最佳答案
首先,将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>
关于asp.net - 在web.config文件中两次使用连接字符串;另一个用于Nlog配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1062842/