NLog not writing to Database Target

4.7k views Asked by At

Having trouble getting the database target for NLog to work. I have 2 targets: FILE and DB.. here is my 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>

And here is the DB design:

enter image description here

And here is the connection string:

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

What am I missing?

2

There are 2 answers

0
Matt On BEST ANSWER

Alright, I figured it out... When I opened up SQL Profiler, I could see the INSERT command was indeed being sent, so I attempted to run the same command myself manually and discovered that I was being an idiot; basically, my Id column is setup as a uniqueidentifier and so of course it could not be auto incremented. The solution was to change the commandText in the Nlog.config to the following:

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)">
0
Bambam Deo On

I got into same problem, reason was dbProvider value and connectionStringName key in nlog.config. I got it from here

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

Also,

  • connectionString should be used in place of connectionStringName
  • if Microsoft.Data.SqlClient is not added in dependency, it need to be added either using nuget package manager or manually.