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

2023-11-27

这是我的 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>

这会起作用并且将插入记录到数据库中。但是我想使用连接字符串名称而不是重新输入连接字符串。 当我改变连接字符串 to 连接字符串名称像这样....

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

to

connectionStringName="ApplicationConnectionString" 

我收到一个错误 'providerInvariantName' 参数需要非空字符串


Add System.Data.SqlClient归因ProviderName在 web.config/app.config 的连接字符串中:

<add name="ApplicationConnectionString" 
providerName="System.Data.SqlClient"
connectionString="server=.\SQLExpress;database=Movie;integrated security=true;"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

nlog 使用 ConnectionStringName 进行数据库日志记录 的相关文章

随机推荐