2017-05-20 184 views
1

我正在使用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> 

记录正常,当我使用v2.2.7 Npgsql的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.0的哪些变化?请帮助在v3.x上使用正确的方法。

在此先感谢。

+0

李善雄列定义: “ENTRY_DATE” 时间戳没有时区 – Fatih

+0

FYI ,来自NLog数据库目标的所有数据库参数都是文本类型。也许你需要在查询中解析。 – Julian

+0

@Julian谢谢你的回复。考虑到“NLog数据库目标是文本类型”,可能我需要编写自定义记录器类。但是我想知道是否有一种方法在迁移到Npgsql新的3.x版本时没有代码更改需要,而Nlog 4.x正确地使用Npgsql 2.2.7。 – Fatih

回答

0

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

+0

谢谢@朱连。转换函数适用于SQL语句。 – Fatih

相关问题