2
votes

I use nlog dll(version 3.1.0.0) 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:

Error Error when writing to database System.ArgumentException: Value does not fall within the expected range. at Oracle.ManagedDataAccess.Client.OracleCommand.set_CommandType(CommandType value) at NLog.Targets.DatabaseTarget.WriteEventToDatabase(LogEventInfo logEvent) at NLog.Targets.DatabaseTarget.Write(LogEventInfo logEvent)

the code is:

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

private static LogLevel SetLogLevel(Level level)
    {
        switch (level)
        {
            case Level.Debug: return LogLevel.Debug;
            case Level.Error: return LogLevel.Error;
            case Level.Fatal: return LogLevel.Fatal;
            case Level.Info: return LogLevel.Info;
            default: return LogLevel.Error;
        }

    }

 private void SetPropGDC(LogEntity LogEntity)
    {
        GlobalDiagnosticsContext.Set("connectionString", _unitOfWork.getConnectionString());
        GlobalDiagnosticsContext.Set(processId, LogEntity.PROCESS_ID.ToString());
        GlobalDiagnosticsContext.Set("TIME_STAMP", LogEntity.TIME_STAMP.ToString());
        GlobalDiagnosticsContext.Set(customerId, LogEntity.CUSTOMER_ID.ToString());
        GlobalDiagnosticsContext.Set("REQUEST", LogEntity.REQUEST.ToString());
        GlobalDiagnosticsContext.Set("RESPONSE", LogEntity.RESPONSE.ToString());
        GlobalDiagnosticsContext.Set("EXCEPTION", LogEntity.EXCEPTION.ToString());
        GlobalDiagnosticsContext.Set("STACK_TRACE", LogEntity.STACK_TRACE.ToString());
        GlobalDiagnosticsContext.Set("DETAILS", LogEntity.DETAILS.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,LOG_LEVEL,REQUEST,RESPONSE,EXCEPTION,STACK_TRACE,MESSAGE)
                       values(:PROCESS_ID,:TIME_STAMP,:CUSTOMER_ID,:LOG_LEVEL,:REQUEST,:RESPONSE,:EXCEPTION,:STACK_TRACE,:MESSAGE)">
  <parameter name="PROCESS_ID" layout="${gdc:PROCESS_ID}" />
  <parameter name="TIME_STAMP" layout="${gdc:TIME_STAMP}" />
  <parameter name="CUSTOMER_ID" layout="${gdc:CUSTOMER_ID}" />
  <parameter name="LOG_LEVEL" layout="${level:uppercase=true}" /> 
  <parameter name="REQUEST" layout="${gdc:REQUEST}" />
  <parameter name="RESPONSE" layout="${gdc:RESPONSE}" />
  <parameter name="EXCEPTION" layout="${gdc:EXCEPTION}" />
  <parameter name="STACK_TRACE" layout="${gdc:STACK_TRACE}" />
  <parameter name="MESSAGE" layout="${message}" />
</target>

Can anyone help?

1
The error messages from NET library are not very good. The best way of solving issues like this is to get the string query and run the query in the Oracle Query Tool and see the error. Usually Date issues occur when the format is wrong. For example using Month/Day/Year (US) instead of Day/Month/Year (England). You are using TimeStamp.ToString() where it is preferable to create a parameter so you are not dependent on the Culture Settings of the PC where you may get a US date in England.jdweng
all the field in the table are or varchar or number. and I run the query in the oracle and the line was inserted so I dont think that the problem is in the queryrikush
You need to compare the format of the dates in the oracle query with the format of the dates in c# and see if they are the same. You may need to remove from the following the "ToString() if the dates in Oracle are stored as DateTime : TIME_STAMP.ToString()jdweng

1 Answers

1
votes

I think you're looking for type support for database parameters, that's introduced in NLog 4.6.

PS: Please note that event properties (on LogEventInfo) are a more robust than the GlobalDiagnosticsContext in your example