gpt4 book ai didi

c# - nlog4.x ${longdate} 适用于 Npgsql 2.2.7 但不适用于 Npgsql 3.x

转载 作者:行者123 更新时间:2023-11-29 13:19:46 24 4
gpt4 key购买 nike

我正在使用 NLog4.1 版本进行日志记录。

我的“NLog.config”看起来像:

<target name="database" xsi:type="Database">
<connectionStringName>PRODUCTION</connectionStringName>
<commandText>
INSERT INTO "MY"."LOG_TABLE" ( "ENTRY_DATE" ) VALUES ( @ENTRY_DATE );
</commandText>

<parameter name="@entry_date" layout="${longdate}"/>
</target>

当我使用 Npgsql v2.2.7 Nuget 包时,日志记录正常。

但是当升级 Nuget 包到 Npgsql v3.2.2和 EntityFramework6.Npgsql 到 v3.1.1 日志记录给出错误:

protected static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
Logger.Info("Logger started.");

以下错误:

42804: column "ENTRY_DATE" is of type timestamp without time zone but expression is of type text

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Npgsql.PostgresException: 42804: column "ENTRY_DATE" is of type timestamp without time zone but expression is of type text

[PostgresException (0x80004005): 42804: column "ENTRY_DATE" is of type timestamp without time zone but expression is of type text]

网络配置更改

对于 v2.2.7:

<system.data>
<DbProviderFactories>
<add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
</providers>
</entityFramework>

对于 v3.2.2:

<system.data>
<DbProviderFactories>
<add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>
</entityFramework>

Postgresql 数据库列定义:

 "ENTRY_DATE" timestamp without time zone

升级到 v3.x 后有什么变化?请帮助在 v3.x 上以正确的方式使用。

提前致谢。

最佳答案

更新的答案

从 NLog 4.6 开始,可以使用 dbType 属性配置参数类型。 NLog 4.6 已于 2019 年 3 月发布。

例如

<parameter name="@entry_date" layout="${longdate}" dbType="DateTime" />

参见 https://github.com/NLog/NLog/wiki/Database-target

dbType - One of the values of DbType (e.g. "Int32", "Decimal", "DateTime"), or a value of DBType like prefixed with the property name, e.g. "SqlDbType.NChar" will set the property "SqlDbType" on "NChar". Another example: "NpgsqlDbType.Json" with NpgsqlParameter. Introduced in NLog 4.6. Since NLog 4.7 not case-sensitive any more

原答案:

NLog中来自数据库目标的参数是文本参数。

看来现在Npgsql/PostgreSQL/EntityFramework6比较挑剔了。

您需要将string 转换为timestamp 类型。您可以使用 to_timestamp PostgreSQL 函数在查询中执行此操作:

 <commandText>
INSERT INTO "MY"."LOG_TABLE" ( "ENTRY_DATE" )
VALUES (to_timestamp(@ENTRY_DATE, 'YYYY-MM-DD H24:MI:SS.MS');
</commandText>

C#格式:yyyy-MM-dd HH:mm:ss.ffff,PostgreSQL格式:YYYY-MM-DD H24:MI:SS.MS - 参见 PostgreSQL docs

关于c# - nlog4.x ${longdate} 适用于 Npgsql 2.2.7 但不适用于 Npgsql 3.x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44088877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com