NLog adding a database schema name

1

I am unsure if its possible to add a database schema to NLog. I have tried this method:

<?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"
      internalLogLevel="Info"
      internalLogFile="C:\temp\internallog.txt">
  <targets>
    <target name="logfile" xsi:type="File"
            fileName="C:\temp/${shortdate}_logfile.txt"
            layout="${longdate} ${level:uppercase=true} ${message}"/>


    <target xsi:type="Database" name="database"
            connectionString="Server=.\SQLEXPRESS;Database=AppDB;Trusted_Connection=True;MultipleActiveResultSets=true;User Id=AppDBUser;Password=PSSWRD!"
            commandType="StoredProcedure"
            commandText="[AppDB].[ASTONE.EventLogs]">
        <parameter name="@message" layout="${message}" />
        <parameter name="@level" layout="${level}" />
        <parameter name="@logger" layout="${logger}" />
        <!--<dbProvider>System.Data.SqlClient</dbProvider>-->
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="logfile" />
    <logger name="*" minlevel="Info" writeTo="database" />
  </rules>
</nlog>

However, this gives me this error:

Error DatabaseTarget(Name=database): Error when writing to database. Exception: System.Data.SqlClient.SqlException (0x80131904): Could not find stored procedure 'AppDB.ASTONE.EventLogs'.

I have looked through the NLog wiki but it doesn't seem to have anything as well.

asp.net
logging
asp.net-core
nlog
asked on Stack Overflow Dec 28, 2018 by JianYA

1 Answer

3

This is from memory, but I think your commandText value would need to be this:

commandText="[ASTONE].[EventLogs]"

The separating . needs to be outside the square brackets.

Since you specify the database name in the connection string, you shouldn't need it as part of the command text.

UPDATED WITH NEW INFORMATION

Since ASTONE.EventLogs is a table, you will need to write a stored procedure to insert your parameter values into it. That's your missing piece.

Here's an example stored procedure:

CREATE PROC [ASTONE].[LogEvent] 
(
   @message nvarchar(max),
   @level   int,
   @logger  nvarchar(1024)
)
AS 
  INSERT INTO [ASTONE].[EventLogs]
  ( message, level, logger )
  VALUES( @message, @level, @logger)

I am guessing about the layout of your EventLogs table and the types and definitions of the parameters.

And then your commandText attribute would look like this:

 commandText="[ASTONE].[LogEvent]"
answered on Stack Overflow Dec 28, 2018 by Ann L. • edited Dec 29, 2018 by Ann L.

User contributions licensed under CC BY-SA 3.0