Colonel_Custard
Colonel_Custard

Reputation: 1390

How do I put values from nlog into a database?

I want to get values from nlog and put them into a database, however when I run my application with the current code I have it throws an exception with the following message;

Must declare the scalar variable \"@MachineName\"."}

this is my nlog.config file, any help would be appreciated =]

<?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">

  <!-- 
  See http://nlog-project.org/wiki/Configuration_file 
  for information on customizing logging rules and outputs.
   -->

  <targets>
    <target xsi:type="Database"
      name="Database"
      dbProvider="sqlserver"
      useTransactions="true"
      connectionString="Data Source=AC-02\SQLEXPRESS;Initial Catalog=Modelfirst.Blogging;Integrated Security=True"
      keepConnection="true"
      commandText="INSERT INTO Logs (Machine_Name, Username, Logon_Time, Screensaver_On, Screensaver_Off, Logoff_Time, Program_Start) Values (@MachineName, @Username, @LogonTime, @Screensaver_On, @Screensaver_Off, @LogoffTime, @ProgramStart)"

            />

  </targets>



  <rules>
    <logger name="*" minlevel="Trace" writeTo="Database" />
  </rules>
</nlog>

Upvotes: 0

Views: 670

Answers (2)

Simon Halsey
Simon Halsey

Reputation: 5480

Presumably you need to add <parameter layout="Layout" name="String" precision="Byte" scale="Byte" size="Integer"/> to declare what all you parameters mean?

@MachineName fails because it's the first 1.

You need to declare a parameter for each parameter in your insert statement & in layout state what nlog variable it should be.

so:

<parameter layout="${machinename}" name="@MachineName" size="50"/><!-- repeated -->

this is the documentation for the database target. Add an element as above for each parameter.

Upvotes: 1

DGibbs
DGibbs

Reputation: 14618

Looks like a misspelled parameter in your command text, Machine_Name and @MachineName are different.

Upvotes: 2

Related Questions