3

Closed

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

file attachments

Closed Oct 7, 2012 at 5:56 PM by Xharze
Moved to https://github.com/NLog/NLog/issues/141

comments

BugNuker wrote Dec 1, 2011 at 5:38 PM

it works fine in 2.0
Here is a sample:
<target xsi:type="Database" name="database" connectionString="Data Source=.\;Initial Catalog=NorthWind;Integrated Security=True" commandText="exec uspInsertSystemLog
  @Level,
  @SystemUserID,
  @TransactionID,
  @Status,
  @Description,
  @Machine,
  @ProcessID,
  @ThreadID,
  @StackTrace,
  @Logger">

  <parameter name="@Level" layout="${level}"/>
  <parameter name="@SystemUserID" layout="${event-context:item=SystemUserID}"/>
  <parameter name="@TransactionID" layout="${event-context:item=TransactionID}"/>
  <parameter name="@Status" layout="${message}"/>
  <parameter name="@Description" layout="${event-context:item=Description}"/>
  <parameter name="@Machine" layout="${machinename}"/>
  <parameter name="@ProcessID" layout="${processID}"/>
  <parameter name="@ThreadID" layout="${threadid}"/>
  <parameter name="@StackTrace" layout="${stacktrace}"/>
  <parameter name="@Logger" layout="${logger}"/>
</target>

pespespes wrote Apr 26, 2012 at 5:05 PM

Esta como tipo Text
Solucion

commandText="EXECUTE dbo.aspnet_NLog_LogError
@Application=@Application
,@LogTime=@LogTime
,@Level=@Level
,@Message=@Message
,@Exception=@Exception"