简体   繁体   中英

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>

看起来像你的命令文本拼错的参数, Machine_Name@MachineName是不同的。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM