NLog 2 Beta 1 - CommandType in Database Target - Stored Procedure Issue
description
Hi,
I did not have success in NLog 2 Beta1 in using the database target to log to sql database via a stored procedure. Here is my nlog.config:
<?xml version="1.0"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true" internalLogFile="d:\x.log" internalLogLevel="Trace">
<targets>
<target xsi:type="Database"
name="SqlLogTarget"
connectionString="Data Source=machinename\sqlexpress;Initial Catalog=dotnetlogs;Integrated Security=SSPI;Persist Security Info=False;"
commandText="[dbo].[aspnet_NLog_LogError]"
dbProvider="sqlserver">
<parameter name="@Application" layout="myapp"/>
<parameter name="@LogTime" layout="${longdate}"/>
<parameter name="@Level" layout="${level}"/>
<parameter name="@Message" layout="${message}"/>
<parameter name="@Exception" layout="${exception}"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="SqlLogTarget" />
</rules>
</nlog>
...NLog's debug trace revelead this:
2010-11-10 21:11:34.3984 Trace Executing Text: [dbo].[aspnet_NLog_LogError]
2010-11-10 21:11:34.3984 Trace Parameter: '@Application' = 'lts' (String)
2010-11-10 21:11:34.3984 Trace Parameter: '@LogTime' = '2010-11-10 21:11:34.3984' (String)
2010-11-10 21:11:34.3984 Trace Parameter: '@Level' = 'Trace' (String)
2010-11-10 21:11:34.3984 Trace Parameter: '@Message' = 'inside page load' (String)
2010-11-10 21:11:34.3984 Trace Parameter: '@Exception' = '' (String)
2010-11-10 21:11:34.4154 Error Error when writing to database System.Data.SqlClient.SqlException: Procedure or function 'aspnet_NLog_LogError' expects parameter '@Application', which was not supplied.
<--lines omitted for brevity-->
<--lines omitted for brevity-->
at NLog.Targets.DatabaseTarget.WriteEventToDatabase(LogEventInfo logEvent) in D:\Desktop\NLog2.source-Beta1\src\NLog\Targets\DatabaseTarget.cs:line 485
at NLog.Targets.DatabaseTarget.Write(LogEventInfo logEvent) in D:\Desktop\NLog2.source-Beta1\src\NLog\Targets\DatabaseTarget.cs:line 376
--------------------
From the first line it seems that commandType is always 'Text'. I downloaded the source code and set -- command.CommandType = System.Data.CommandType.StoredProcedure -- and it worked.
It seems that we need a 'commandType' attribute in the config that can later read and be set in NLog.Targets.DatabaseTarget.WriteEventToDatabase method (probably after setting the commandText).
I have attached a zip containing the web project alongwith a script file "NLog_SqlScripts.txt" containing the db table & proc
Vickram