简体   繁体   English

NLog-写入数据库时​​未传递给查询的参数

[英]NLog - Parameters not passed to query when writing to database

I'm pretty new to NLog so please forgive my basic question. 我是NLog的新手,请原谅我的基本问题。

I've inherited a Winforms application written by some contractors, I'm mainly a database developer but I've managed to do some development of the application but I sometimes struggle with tracking down error messages encountered by users, therefore I'm attempting to retrofit logging into the application via NLog, I first encountered the issue in NLog 4.4.5 and I've since updated to 4.4.12 and that hasn't solved the problem. 我继承了一些承包商编写的Winforms应用程序,我主要是数据库开发人员,但是我设法完成了该应用程序的一些开发,但是有时我很难跟踪用户遇到的错误消息,因此我试图通过NLog改造登录到应用程序中,我首先在NLog 4.4.5中遇到了问题,此后我已更新到4.4.12,但并没有解决问题。 I've verified that NLog is catching the errors correctly as it will output to a text file but when I try to direct it to a database output I can't get it to work. 我已经验证NLog正确地捕获了错误,因为它将输出到文本文件,但是当我尝试将其定向到数据库输出时,我无法使其正常工作。

This is my database table: 这是我的数据库表:

SQL表

My problem is that I can only get errors written to the database only if I don't pass any parameters to the insert statement (which is pretty useless). 我的问题是,只有在不将任何参数传递给insert语句的情况下,我才能将错误写入数据库(这几乎没有用)。 That is to say that the following in my NLog.config file works: 也就是说,我的NLog.config文件中的以下内容有效:

<target name="database" xsi:type="Database">
  <commandText>INSERT INTO [tblException] (DbVersionID, ExceptionDateTime) SELECT MAX(DbVersionID), GETDATE() FROM tblDbVersion</commandText>

  <dbProvider>System.Data.SqlServerCe.4.0</dbProvider>
  <connectionString>Data Source=${basedir}\Database.sdf</connectionString>
</target>

But this doesnt': 但这不是':

<target name="database" xsi:type="Database">
   <commandText>INSERT INTO [tblException] (DbVersionID, ExceptionDateTime, Message) SELECT MAX(DbVersionID), GETDATE(), @message FROM tblDbVersion</commandText>

   <parameter name="@message" layout="${message}" />

   <dbProvider>System.Data.SqlServerCe.4.0</dbProvider>
   <connectionString>Data Source=${basedir}\Database.sdf</connectionString>
</target>

I've enabled internal logging and the following is what I get: 我启用了内部日志记录,以下是我得到的:

2017-11-28 11:26:45.8063 Trace Executing Text: INSERT INTO [tblException] (DbVersionID, ExceptionDateTime, Message) SELECT MAX(DbVersionID), GETDATE(), @Message FROM tblDbVersion
2017-11-28 11:26:45.8063 Trace   Parameter: '@message' = 'Test Error Message' (String)
2017-11-28 11:26:45.8063 Error Error when writing to database. Exception: System.Data.SqlServerCe.SqlCeException (0x80004005): A parameter is not allowed in this location. Ensure that the '@' sign is in a valid location or that parameters are valid at all in this SQL statement.
   at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)
   at System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan()
   at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
   at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery()
   at NLog.Targets.DatabaseTarget.WriteEventToDatabase(LogEventInfo logEvent)
   at NLog.Targets.DatabaseTarget.Write(LogEventInfo logEvent)

It appears that the parameter isn't getting replaced in the query before it's being run. 看来该参数在运行之前并未在查询中被替换。 I tried adding single quotes in the command text just in case that would help but it just resulted in the literal string '@message' being inserted into the database field. 我试图在命令文本中添加单引号,以防万一有帮助,但这只是导致将文字字符串'@message'插入数据库字段中。

I can't see anything that I've done differently to the examples, so any help would be appreciated. 我看不到我所做的任何与示例不同的事情,因此将不胜感激。

Regards, 问候,

Alex 亚历克斯

As per the comment from @pmcilreavy 根据@pmcilreavy的评论

Have you tried temporarily removing the MAX and replacing with a literal and changing to INSERT INTO .. (blah) VALUES (blah, @Message); 您是否尝试过临时删除MAX并替换为文字并更改为INSERT INTO ..(等等)值(等等,@ Message); SqlCe has quite a few limitations and quirks compared to Sql Server so might be worth simplifying things where possible. 与Sql Server相比,SqlCe具有许多限制和怪癖,因此可能值得简化。

The issue appears to be that SQLCE doesn't support parameters in the select part of a select statement. 问题似乎是SQLCE在select语句的select部分中不支持参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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