[英]How do I put values from nlog into a database?
我想从nlog中获取值并将它们放入数据库中,但是当我使用当前代码运行应用程序时,它会抛出异常并显示以下消息;
Must declare the scalar variable \"@MachineName\"."}
这是我的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">
<!--
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
是不同的。
大概您需要添加<parameter layout="Layout" name="String" precision="Byte" scale="Byte" size="Integer"/>
来声明所有参数的含义?
@MachineName失败,因为它是第一个。
您需要为您的插入语句中的每个参数声明一个参数,并在布局状态下为该参数指定nlog变量。
所以:
<parameter layout="${machinename}" name="@MachineName" size="50"/><!-- repeated -->
这是数据库目标的文档 。 为每个参数添加上述元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.