Nlog未写入ORACLE Database .net 6 Web应用程序(后续问题)

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

我在我的.NET 6应用程序中配置了Nlog,它正确地记录到文件和控制台,但是当我试图写入我的ORACLE数据库时,它在Nlog内部日志文件中抛出表或视图不存在的错误(当我的表存在于数据库中时)。
下面是我的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(LogLevel, Message, CreatedOn, Exception, URL, Logger, Action) values(:level, :message, :createddate, :exception, :url, :logger, :action)"
            commandType ="Text">
            <parameter name="level" layout="${level}" />
            <parameter name="message" layout="${message}" />
            <parameter name="createddate" 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(LogLevel, Message, CreatedOn, Exception, URL, Logger, Action) values(:level, :message, :createddate, :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: 'createddate' = '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 09:25:12.2648 Error DatabaseTarget(Name=database): Error when writing to database. Exception: Oracle.ManagedDataAccess.Client.OracleException (0x80004005): ORA-00942: table or view does not exist
   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)
2023-11-08 09:25:12.2648 Trace DatabaseTarget(Name=database): Close connection because of error
2023-11-08 09:25:12.2648 Trace DatabaseTarget(Name=database): Close connection (KeepConnection = false).


已验证连接字符串与appsettings.json文件中的连接字符串相同,并且还在数据库内创建了表,如Table is present in the database下图所示

tp5buhyn

tp5buhyn1#

根据您包含的图像,您已经创建了带有引用标识符"LogTable"的日志表-否则它将在数据字典中显示为LOGTABLE,而不是LogTable
如果你有一个带引号的标识符,那么你需要引用它的所有引用,所以你的插入将是insert into "LogTable"(...,如果你是这样定义表的话,也可能用带引号的列名:insert into "LogTable"("LogLevel", "Message", ...
但这意味着您需要更改XML,以便不必escape那些双引号字符:

commandText = 'insert into "LogTable"("LogLevel", "Message", "CreatedOn", "Exception", "URL", "Logger", "Action") values(:level, :message, :createddate, :exception, :url, :logger, :action)'

字符串
带引号的标识符通常是一个痛苦的工作,所以如果你有选择,我会建议你删除和重新创建你的表没有他们;即,而不是

create table "LogTable" ("LogLevel" ...


create table LogTable (LogLevel ...


.然后你原来的(未加引号的)插入语句就可以工作了。

相关问题