映射器 [NpgsqlValue] 不能从类型 [date] 更改为 [ObjectMapper]

mapper [NpgsqlValue] cannot be changed from type [date] to [ObjectMapper]

我正在尝试将 Serilog 生成的日志文件中的异常导入 ElasticSearch,我收到 400 错误消息:

"type": "illegal_argument_exception",
"reason": "mapper [fields.ExceptionDetail.Statement.InputParameters.Collection.NpgsqlValue] cannot be changed from type [date] to [ObjectMapper]"

这里是 json 例外情况:

{
   "@timestamp":"2021-10-23T18:07:01.3922829+03:00",
   "level":"Error",
   "messageTemplate":"Duplicate value",
   "message":"Duplicate value",
   "exceptions":[
      {
         "Depth":0,
         "ClassName":"Npgsql.PostgresException",
         "Message":"23505: duplicate key value violates unique constraint \"Table1_pkey\"",
         "Source":"Npgsql",
         "StackTraceString":"   at Npgsql.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|194_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)\r\n   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)\r\n   at Npgsql.NpgsqlDataReader.NextResult()\r\n   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)\r\n   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)\r\n   at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)\r\n   at Npgsql.NpgsqlCommand.ExecuteNonQuery()\r\n   at Dapper.Logging.Hooks.WrappedCommand`1.ExecuteNonQuery()\r\n   at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action`2 paramReader)\r\n   at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command)\r\n   at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType)\r\n   at Dapper.FastCrud.SqlStatements.GenericSqlStatements`1.InsertAsync(IDbConnection connection, TEntity entity, AggregatedSqlStatementOptions`1 statementOptions) in D:\a\1\s\Dapper.FastCrud\SqlStatements\GenericSqlStatements.cs:line 112\r\n   at SecurityIntegration.Domain.Database.DbRepository.InsertTable1() in C:\Projects\security-integration\security-integration-git\securityIntegration.Domain\Database\DbRepository.cs:line 64\r\n   at SecurityIntegration.Domain.Services.MainService.Action(ActionRequest request, DateTime receivedTimestamp) in C:\Projects\security-integration\security-integration-git\securityIntegration.Domain\Services\MainService.cs:line 36\r\n   at SecurityIntegration.customer.customerService.Action(ActionRequest request, DateTime receivedTimestamp) in C:\Projects\security-integration\security-integration-git\securityIntegration.customer\customerService.cs:line 34\r\n   at lambda_method5(Closure , Object )\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)\r\n   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)\r\n   at SecurityIntegration.Infrastructure.Middleware.LoggingMiddleware.Invoke(HttpContext context) in C:\Projects\security-integration\security-integration-git\securityIntegration.Infrastructure\Middleware\LoggingMiddleware.cs:line 25\r\n   at SecurityIntegration.Infrastructure.Middleware.CorrelationIdMiddleware.Invoke(HttpContext httpContext) in C:\Projects\security-integration\security-integration-git\securityIntegration.Infrastructure\Middleware\CorrelationIdMiddleware.cs:line 61\r\n   at SecurityIntegration.Infrastructure.Middleware.ErrorHandlerMiddleware.Invoke(HttpContext context) in C:\Projects\security-integration\security-integration-git\securityIntegration.Infrastructure\Middleware\ErrorHandlerMiddleware.cs:line 35",
         "RemoteStackTraceString":null,
         "RemoteStackIndex":0,
         "HResult":-2147467259,
         "HelpURL":null
      }
   ],
   "fields":{
      "SourceContext":"SecurityIntegration.Infrastructure.Middleware.ErrorHandlerMiddleware",
      "RequestId":"0HMCM6R1JP88C:00000002",
      "RequestPath":"/v1/security/customer/Action",
      "ConnectionId":"0HMCM6R1JP88C",
      "ExceptionDetail":{
         "Data":{
            "Severity":"ERROR",
            "InvariantSeverity":"ERROR",
            "SqlState":"23505",
            "MessageText":"duplicate key value violates unique constraint \"Table1_pkey\"",
            "Detail":"Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.",
            "SchemaName":"public",
            "TableName":"Table1",
            "ConstraintName":"Table1_pkey",
            "File":"d:\pginstaller_12.auto\postgres.windows-x64\src\backend\access\nbtree\nbtinsert.c",
            "Line":"570",
            "Routine":"_bt_check_unique"
         },
         "HResult":-2147467259,
         "Message":"23505: duplicate key value violates unique constraint \"Table1_pkey\"",
         "Source":"Npgsql",
         "IsTransient":false,
         "Statement":{
            "SQL":"INSERT INTO \"Table1\" (\"Parameter1\",\"Parameter2\",\"Parameter3\") VALUES (,,) ",
            "StatementType":"Unknown",
            "Rows":0,
            "LongRows":0,
            "OID":0,
            "InputParameters":[
               {
                  "ConvertedValue":null,
                  "ParameterName":"Parameter1",
                  "Value":"79cdec6e-0c7b-49d7-b0dd-1c5755b833b1",
                  "NpgsqlValue":"79cdec6e-0c7b-49d7-b0dd-1c5755b833b1",
                  "DbType":"String",
                  "NpgsqlDbType":"Text",
                  "DataTypeName":"threw System.NotImplementedException: Infer from others",
                  "IsNullable":false,
                  "Direction":"Input",
                  "Precision":0,
                  "Scale":0,
                  "Size":4000,
                  "SourceColumn":"",
                  "SourceVersion":"Current",
                  "SourceColumnNullMapping":false,
                  "$id":"1",
                  "Collection":[
                     {
                        "$ref":"1"
                     },
                     {
                        "ConvertedValue":null,
                        "ParameterName":"Parameter2",
                        "Value":{
                           "_typeTag":"DBNull"
                        },
                        "NpgsqlValue":{
                           "_typeTag":"DBNull"
                        },
                        "DbType":"String",
                        "NpgsqlDbType":"Text",
                        "DataTypeName":"threw System.NotImplementedException: Infer from others",
                        "IsNullable":false,
                        "Direction":"Input",
                        "Precision":0,
                        "Scale":0,
                        "Size":0,
                        "SourceColumn":"",
                        "SourceVersion":"Current",
                        "SourceColumnNullMapping":false,
                        "Collection":{
                           "$ref":"Cyclic reference"
                        },
                        "PostgresType":null,
                        "$id":"2"
                     },
                     {
                        "ConvertedValue":null,
                        "ParameterName":"Parameter3",
                        "Value":"2021-10-23T18:07:00.7187309+03:00",
                        "NpgsqlValue":"2021-10-23T18:07:00.7187309+03:00",
                        "DbType":"DateTime",
                        "NpgsqlDbType":"Timestamp",
                        "DataTypeName":"threw System.NotImplementedException: Infer from others",
                        "IsNullable":false,
                        "Direction":"Input",
                        "Precision":0,
                        "Scale":0,
                        "Size":0,
                        "SourceColumn":"",
                        "SourceVersion":"Current",
                        "SourceColumnNullMapping":false,
                        "Collection":{
                           "$ref":"Cyclic reference"
                        },
                        "PostgresType":null,
                        "$id":"3"
                     }
                  ],
                  "PostgresType":null
               },
               {
                  "$ref":"2"
               },
               {
                  "$ref":"3"
               }
            ]
         },
         "Severity":"ERROR",
         "InvariantSeverity":"ERROR",
         "SqlState":"23505",
         "Code":"23505",
         "MessageText":"duplicate key value violates unique constraint \"Table1_pkey\"",
         "Detail":"Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.",
         "Hint":null,
         "Position":0,
         "InternalPosition":0,
         "InternalQuery":null,
         "Where":null,
         "SchemaName":"public",
         "TableName":"Table1",
         "ColumnName":null,
         "DataTypeName":null,
         "ConstraintName":"Table1_pkey",
         "File":"d:\pginstaller_12.auto\postgres.windows-x64\src\backend\access\nbtree\nbtinsert.c",
         "Line":"570",
         "Routine":"_bt_check_unique",
         "ErrorCode":-2147467259,
         "Type":"Npgsql.PostgresException"
      },
      "MachineName":"MACHINE-NAME"
   }
}

我相信这是因为 NpgsqlValue 具有所有这些不同的值 (text/ObjectMapper/date):

"NpgsqlValue":"79cdec6e-0c7b-49d7-b0dd-1c5755b833b1"
"NpgsqlValue":{"_typeTag":"DBNull"}
"NpgsqlValue":"2021-10-23T08:31:54.5474090Z"

请问如何解决这个问题?

根据mapper [NpgsqlValue] cannot be changed from type [date] to [ObjectMapper]解决所描述的问题我有2个选择:

  1. 通过从配置文件中删除“WithExceptionDetails”并等待 Postgres 异常的析构器出现,从我的日志中删除异常的详细信息。
  2. 编写自定义解构器。