add record nlog date get error invalid month

1

I use nlog dll to write to database - oracle with entity frameWork in the line :

logger.Log(logLevel, "try");  

I get in the logs of NLog the following error:

Warn DatabaseTarget: Parameter: 'TIME_STAMP' - Failed to assign DbType=OracleDbType.Date Error DatabaseTarget(Name=WS_TRACE): Error when writing to database. Exception: System.Data.OracleClient.OracleException (0x80131938): ORA-01843: Invalid month

the code is:

 SetPropGDC(LogEntity);
 NLog.LogLevel logLevel = SetLogLevel(Level.Debug);
 logger.Log(logLevel, "try");
 ClearGDC();

 private void SetPropGDC(LogEntity LogEntity)
    {
        GlobalDiagnosticsContext.Set(processId, LogEntity.PROCESS_ID.ToString());
        GlobalDiagnosticsContext.Set("TIME_STAMP", DateTime.Now);
        GlobalDiagnosticsContext.Set(customerId, LogEntity.CUSTOMER_ID.ToString());

    }
  <targets>
    <target name="TRACEDatabase" type="DataBase"  keepConnection="false" 
             dbProvider="Oracle.ManagedDataAccess.Client" connectionString="${gdc:connectionString}"
             commandText="insert into TLOG_SITE_GENERAL_TRACE( PROCESS_ID,TIME_STAMP,CUSTOMER_ID)
                           values(:PROCESS_ID,:TIME_STAMP,:CUSTOMER_ID)">
      <parameter name="PROCESS_ID" layout="${gdc:PROCESS_ID}" />
      <parameter name="TIME_STAMP" layout="${gdc:TIME_STAMP}" dbType="OracleDbType.Date" />
      <parameter name="CUSTOMER_ID" layout="${gdc:CUSTOMER_ID}" />

    </target>
  </targets>

I tried in the Web.config to change the line:

<parameter name="TIME_STAMP" layout="${gdc:TIME_STAMP}" dbType="OracleDbType.Date" />

to:

<parameter name="TIME_STAMP" layout="${longDate}" dbType="OracleDbType.Date" />

and I got the same error

c#
oracle
date
nlog
asked on Stack Overflow May 10, 2020 by rikush • edited May 10, 2020 by Julian

2 Answers

1

First of all, it's highly recommend to use the built in layout renderers.

For example, ${longdate}, but also ${processid}. Those are optimized and easier to use. You can find all of them here

Be aware is that using global context (GDC), could be dangerous in multi threaded programs. Also in single threaded you could "leak" some context.

But I guess the real issue here is that your NLog is out to date. DbType support is added in NLog 4.6.

So first update NLog (using Nuget is highly recommend). I use here NLog 4.6.3+, but would recommend to update to NLog 4.7

Then for the code I would recommend to do this:

logger.WithProperty("CustomerId", LogEntity.CUSTOMER_ID).Log(loglevel, "Message");

and configure like this:

<targets>
    <target name="TRACEDatabase" type="DataBase"  keepConnection="false" 
             dbProvider="Oracle.ManagedDataAccess.Client" connectionString="${gdc:connectionString}"
             commandText="insert into TLOG_SITE_GENERAL_TRACE( PROCESS_ID,TIME_STAMP,CUSTOMER_ID)
                           values(:PROCESS_ID,:TIME_STAMP,:CUSTOMER_ID)">
      <parameter name="PROCESS_ID" layout="${processid}" dbType="Int32" />
      <parameter name="TIME_STAMP" layout="${longdate}" dbType="Date" />
      <parameter name="CUSTOMER_ID" layout="${event-properties:CUSTOMER_ID}"  dbType="Int32"  />
    </target>
</targets>

See also ${event-properties}

There is no need for using a specific OracleDbType, so therefor I choose to just use "Date" (which is DbType.Date, see DbType

answered on Stack Overflow May 10, 2020 by Julian
1

I found the solution! I changed the dbType parameter from:

 <parameter name="TIME_STAMP" layout="${gdc:TIME_STAMP}" dbType="OracleDbType.Date"  />

To:

 <parameter name="TIME_STAMP" layout="${gdc:TIME_STAMP}" dbType="DateTime" />

and it works!

answered on Stack Overflow May 10, 2020 by rikush • edited May 12, 2020 by rikush

User contributions licensed under CC BY-SA 3.0