NLog getting current user causes error to appear

0

This is my target

<target xsi:type="Database" name="database"
   connectionString="${var:connectionString}"
   commandText="INSERT INTO [AppOne].[EventLogs](Id, Message, Level, Logger, Action,Identity)VALUES(NewID(), @Message, @Level, @Logger, @Action,@Identity)">
<parameter name="@Message" layout="${message}" />
<parameter name="@Level" layout="${level}" />
<parameter name="@Logger" layout="${logger}" />
<parameter name="@Action" layout="${aspnet-mvc-action}"/>
<parameter name="@Identity" layout="${aspnet-user-identity}"/>

I have also placed this line in my Startup.cs

services.AddSingleton<Microsoft.AspNetCore.Http.IHttpContextAccessor, HttpContextAccessor>();

However, when I run my application and try to log an item, I get this error in the internal log:

Error DatabaseTarget(Name=database): Error when writing to database. Exception: System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'Identity'.

I am using Asp.net Core 2.1. I tried searching through the documentation and it looks like this supports asp.net only?

asp.net
logging
asp.net-core
nlog
asked on Stack Overflow Jan 2, 2019 by JianYA • edited Jan 29, 2019 by Rolf Kristensen

1 Answer

1

You have used a keyword as a column name, I would avoid doing this if you can to stop issues like this occurring. You can however resolve the error by wrapping the keyword in square brackets, so your SQL should be

INSERT INTO [AppOne].[EventLogs](Id, [Message], [Level], Logger, [Action],[Identity])VALUES(NewID(), @Message, @Level, @Logger, @Action,@Identity)

answered on Stack Overflow Jan 2, 2019 by Scrobi

User contributions licensed under CC BY-SA 3.0