【问题标题】:How do I put values from nlog into a database?如何将 nlog 中的值放入数据库?
【发布时间】:2016-09-10 05:05:16
【问题描述】:

我想从 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>

【问题讨论】:

    标签: c# sql database nlog


    【解决方案1】:

    在您的命令文本中看起来像是一个拼写错误的参数,Machine_Name@MachineName 是不同的。

    【讨论】:

    • 好答案! =D 但是,它仍然抛出相同的异常,唯一的区别是它现在告诉我没有声明 @Machine_Name 标量变量
    • 我认为@Simon Halsey 可能拥有它
    【解决方案2】:

    大概您需要添加<parameter layout="Layout" name="String" precision="Byte" scale="Byte" size="Integer"/> 来声明所有参数的含义?

    @MachineName 失败,因为它是第一个 1。

    您需要在插入语句中为每个参数声明一个参数,并在布局状态中声明它应该是什么 nlog 变量。

    所以:

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

    这是数据库目标的documentation。为每个参数添加一个如上所述的元素。

    【讨论】:

    • 你能告诉我一个正确语法的例子吗?我是 nlog 的新手 =]
    • 感谢更新的答案,但是我仍然遇到同样的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2021-04-13
    相关资源
    最近更新 更多