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.
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]"
User contributions licensed under CC BY-SA 3.0