使用connectionStringName进行数据库日志记录的nlog

10
这是我的nlog.config文件。我已经启用了throwsException。
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  throwExceptions="true">
      <targets>   
        <target type="Database" name="databaseLog"
        dbProvider="sqlserver"   connectionstring="server=.\SQLExpress;database=Movie;integrated security=true">
            <commandText>
                INSERT INTO [Log] ([Description] , [Level] ) VALUES (@Description,  @Level )
            </commandText>
            <parameter name="@Description" layout="${message}"/> 
            <parameter name="@Level" layout="${level}"/>
        </target>

      </targets>

      <rules>
         <logger name="*" minLevel="Trace"  appendTo="databaseLog"/> 
      </rules>
</nlog>

这样做可以将记录插入数据库。但我想使用connectionstringName而不是重新输入连接字符串。 当我像这样将connectionstring更改为connectionstringname时...

connectionstring="server=.\SQLExpress;database=Movie;integrated security=true"

为了

connectionStringName="ApplicationConnectionString" 

我遇到了一个错误 期望 'providerInvariantName' 参数不为空字符串。
1个回答

14

在web.config / app.config的连接字符串中,在ProviderName属性中添加System.Data.SqlClient


<add name="ApplicationConnectionString" 
providerName="System.Data.SqlClient"
connectionString="server=.\SQLExpress;database=Movie;integrated security=true;"/>

3
请问这个应该放在nLog配置文件还是web.config文件中?谢谢。 - AlexVPerl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接