Nlog未写入到Oracle Database.net 6.0 Web应用程序

m528fe3b  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(125)

我在我的.NET 6应用程序中配置了Nlog,它正确地记录到文件和控制台,但是当我试图写入我的ORACLE数据库时,它在Nlog内部日志文件中抛出MISSING Select关键字错误。
下面是我的nlog.config文件。我使用github上的示例配置了它,并为ORACLE更改了必要的DbProvider。

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      throwConfigExceptions="true"
      internalLogLevel="Trace"
      internalLogFile="c:\Temp\nlog-internal.txt">

  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <targets>
    <!-- Database Target: Logs to Oracle Database -->
    <target name="database" xsi:type="Database"
            dbProvider="Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess"
            connectionString ="Data Source=DataSource;User Id=username;Password=pwd;"
            commandText ="insert into LogTable(Level, Message, CreatedOn, Exception, URL, Logger, Action) values(:level, :message, :date, :exception, :url, :logger, :action)"
            commandType ="Text">
            <parameter name="level" layout="${level}" />
            <parameter name="message" layout="${message}" />
            <parameter name="date" layout="${longdate}" dbType="DateTime"/>
            <parameter name="exception" layout="${exception:format=ToString}" />
            <parameter name="url" layout="${aspnet-request-url}" />
            <parameter name="logger" layout="${logger}" />
            <parameter name="action" layout="${aspnet-mvc-action}" />
    </target>

    <!-- Console Target: Logs to Console -->
    <target name="console" xsi:type="ColoredConsole" layout="${longdate} ${logger} ${level:uppercase=true} - ${message}" />
  <target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-AspNetCore6-own.log"
        layout="${longdate}|${event-properties:item=EventId:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}" />
  </targets>

  <rules>
    <!-- Log all messages to database and console -->
    <logger name="Microsoft.*" maxlevel="off" final="true" />
    <logger name="System.Net.Http.*" maxlevel="off" final="true" />
     <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
    <logger name="*" minlevel="Trace" writeTo="database,console" />
  </rules>
</nlog>

字符串
下面是我的nlog内部日志文件中的错误:

2023-11-08 07:45:15.3687 Trace DatabaseTarget(Name=database): Open connection.
2023-11-08 07:45:15.3756 Trace DatabaseTarget(Name=database): Executing Text: insert into LogTable(Level, Message, CreatedOn, Exception, URL, Logger, Action) values(:level, :message, :date, :exception, :url, :logger, :action)
2023-11-08 07:45:15.3756 Trace   DatabaseTarget: Parameter: 'level' = 'Info' (String)
2023-11-08 07:45:15.3756 Trace   DatabaseTarget: Parameter: 'message' = 'Successfully fetched data' (String)
2023-11-08 07:45:15.3756 Trace   DatabaseTarget: Parameter: 'date' = '11/08/2023 07:45:15' (DateTime)
2023-11-08 07:45:15.3910 Trace   DatabaseTarget: Parameter: 'exception' = '' (String)
2023-11-08 07:45:15.3910 Trace   DatabaseTarget: Parameter: 'url' = 'http://localhost/api/message/get' (String)
2023-11-08 07:45:15.3910 Trace   DatabaseTarget: Parameter: 'logger' = 'Controller' (String)
2023-11-08 07:45:15.3910 Trace   DatabaseTarget: Parameter: 'action' = 'GetPage' (String)
2023-11-08 07:45:15.4523 Error DatabaseTarget(Name=database): Error when writing to database. Exception: Oracle.ManagedDataAccess.Client.OracleException (0x80004005): ORA-00928: missing SELECT keyword
   at OracleInternal.ServiceObjects.OracleConnectionImpl.VerifyExecution(Int32& cursorId, Boolean bThrowArrayBindRelatedErrors, SqlStatementType sqlStatementType, Int32 arrayBindCount, OracleException& exceptionForArrayBindDML, Boolean& hasMoreRowsInDB, Boolean bFirstIterationDone)
   at OracleInternal.ServiceObjects.OracleCommandImpl.ExecuteNonQuery(String commandText, OracleParameterCollection paramColl, CommandType commandType, OracleConnectionImpl connectionImpl, Int32 longFetchSize, Int64 clientInitialLOBFS, OracleDependencyImpl orclDependencyImpl, Int64[]& scnFromExecution, OracleParameterCollection& bindByPositionParamColl, Boolean& bBindParamPresent, OracleException& exceptionForArrayBindDML, OracleConnection connection, Boolean isFromEF)
   at Oracle.ManagedDataAccess.Client.OracleCommand.ExecuteNonQuery()
   at NLog.Targets.DatabaseTarget.ExecuteDbCommandWithParameters(LogEventInfo logEvent, IDbConnection dbConnection, IDbTransaction dbTransaction)
   at NLog.Targets.DatabaseTarget.WriteLogEventSuppressTransactionScope(LogEventInfo logEvent, String connectionString)

o7jaxewo

o7jaxewo1#

LEVEL是Oracle中的保留字,用于分层查询;如果不使用带引号的标识符,则不能将其用作列名。
在尝试创建LogTable表时,您应该已经发现了这一点。
如果你确实想使用Level作为标识符,那么用双引号将其括起来,并确保在标识符中使用与定义表时完全相同的大小写:

insert into LogTable("LEVEL", Message, CreatedOn, Exception, URL, Logger, Action)
  values(:level, :message, :date, :exception, :url, :logger, :action)

字符串
更好的解决方案是不使用保留字作为列名,然后可以继续使用不带引号的标识符。
fiddle

相关问题