Andre Roque
Andre Roque

Reputation: 543

NLog to Database (Oracle)

I'm trying to use NLog to log to an Oracle database, already created the table but when I try to log something I get the exception:

ORA-00928: missing SELECT keyword

My NLog.config file is:

<?xml version="1.0" ?>
<nlog autoReload="true" throwExceptions="true" internalLogFile="${basedir}/App_Data/nlog.txt" internalLogLevel="Debug"
      internalLogToConsole="true">

  <targets>
    <!--Useful for debugging-->
    <target name="filelog" type="File" fileName="C:/App_Data/Site.log"
     layout="${date}: ${message}" />

    <target name="databaselog" type="Database">

      <dbProvider>Oracle.DataAccess.Client</dbProvider>

      <!-- database connection parameters -->
      <!-- alternatively you could provide a single 'connectionstring' parameter -->
      <connectionString>DATA SOURCE=database;PERSIST SECURITY INFO=True;USER ID=user;Password=password;Validate Connection=true</connectionString>

      <commandText>
        insert into RS_LOGTABLE ([log_user],[log_level],[log_date],[log_message]) values(@log_user,@log_level,@log_date,@log_message);
      </commandText>

      <parameter name="@log_user" layout="${message}"/>
      <parameter name="@log_level" layout="${message}"/>
      <parameter name="@log_date" layout="${date}"/>
      <parameter name="@log_message" layout="${message}"/>

    </target>

  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="filelog" />
    <logger name="*" minlevel="Trace" writeTo="databaselog" />
  </rules>

</nlog>

And the exception occurs when I do this:

logger = LogManager.GetCurrentClassLogger();
logger.Debug("Teste logger");

I already tried to do the insert without the brackets and get another exception:

**ORA-00936: missing expression**

Upvotes: 2

Views: 4420

Answers (1)

Jirka Picek
Jirka Picek

Reputation: 599

I guess it's bit late for you, but I hope my solution will be helpful for others.

I changed characters @ for colons at commandText and at parameter tags I removed it completely. I don't use brackets and it started to work.

It should be correct this way:

<commandText>
    insert into RS_LOGTABLE (log_user,log_level,log_date,log_message) values(:log_user,:log_level,:log_date,:log_message);
</commandText>

<parameter name="log_user" layout="${message}"/>
<parameter name="log_level" layout="${message}"/>
<parameter name="log_date" layout="${date}"/>
<parameter name="log_message" layout="${message}"/>

I tested it with dbProvider Oracle.DataAccess.Client.

Warning for others: I had a variable named with reserved word and it throws another exception. More details are here PHP ORA-01745: invalid host/bind variable name Warning

Upvotes: 4

Related Questions