NLog不写入数据库目标

时间:2015-06-15 04:29:35

标签: asp.net-mvc nlog

无法使NLog的数据库目标正常工作。我有2个目标:FILE和DB ..这是我的 NLog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      throwExceptions="false">
  <variable name="appName" value="KoreCMS" />
  <targets async="true">
    <target xsi:type="File"
            name="default"
            layout="${longdate} - ${level:uppercase=true}: ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}"
            fileName="${basedir}\App_Data\Log\Debug.log"
            keepFileOpen="false"
            archiveFileName="${basedir}\App_Data\Log\Debug_${shortdate}.{##}.log"
            archiveNumbering="Sequence"
            archiveEvery="Day"
            maxArchiveFiles="30"/>

    <target xsi:type="Database"
            name="database"
            keepConnection="true"
            useTransactions="true"
            dbProvider="System.Data.SqlClient"
            connectionStringName="DefaultConnection"
            commandText="INSERT INTO Kore_Log(EventDateTime, EventLevel, UserName, MachineName, EventMessage, ErrorSource, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage) VALUES (@EventDateTime, @EventLevel, @UserName, @MachineName, @EventMessage, @ErrorSource, @ErrorClass, @ErrorMethod, @ErrorMessage, @InnerErrorMessage)">
      <parameter name="@EventDateTime" layout="${date:s}" />
      <parameter name="@EventLevel" layout="${level}" />
      <parameter name="@UserName" layout="${aspnet-user-identity}" />
      <parameter name="@MachineName" layout="${machinename}" />
      <parameter name="@EventMessage" layout="${message}" />
      <parameter name="@ErrorSource" layout="${event-context:item=error-source}" />
      <parameter name="@ErrorClass" layout="${event-context:item=error-class}" />
      <parameter name="@ErrorMethod" layout="${event-context:item=error-method}" />
      <parameter name="@ErrorMessage" layout="${event-context:item=error-message}" />
      <parameter name="@InnerErrorMessage" layout="${event-context:item=inner-error-message}" />
    </target>

  </targets>
  <rules>
    <logger name="*" writeTo="default" minlevel="Info" />
    <logger name="*" writeTo="database" minlevel="Info" />
  </rules>
</nlog>

这是数据库设计:

enter image description here

这是连接字符串:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Server=.\SQLExpress2012;Initial Catalog=MyDatabaseName;Integrated Security=True;Persist Security Info=True;MultipleActiveResultSets=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

我错过了什么?

2 个答案:

答案 0 :(得分:0)

好吧,我想通了......当我打开 SQL Profiler 时,我看到确实发送了 INSERT 命令,所以我试图运行同样地命令我自己,发现我是个白痴;基本上,我的 ID 列设置为 uniqueidentifier ,因此当然无法自动递增。解决方案是将 Nlog.config 中的 commandText 更改为以下内容:

commandText="INSERT INTO Kore_Log(Id, EventDateTime, EventLevel, UserName, MachineName, EventMessage, ErrorSource, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage) VALUES (NEWID(), @EventDateTime, @EventLevel, @UserName, @MachineName, @EventMessage, @ErrorSource, @ErrorClass, @ErrorMethod, @ErrorMessage, @InnerErrorMessage)">

答案 1 :(得分:0)

我遇到了同样的问题,原因是nlog.config中的 dbProvider 值和 connectionStringName 键。我是从here

获得的
    <target name="database" xsi:type="Database" connectionString="${configsetting:name=sqlconnection.connectionString}"
        dbProvider="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient">

  • connectionString应该代替connectionStringName
  • 如果未以依赖关系添加Microsoft.Data.SqlClient,则需要使用nuget软件包管理器或手动添加。