nlog4.x ${longdate} works on Npgsql 2.2.7 but does not work on Npgsql 3.x

2

I am using NLog4.1 version for logging.

My "NLog.config" looks like:

<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>

Logging is OK when I use Npgsql v2.2.7 Nuget package.

But when upgrade Nuget package to Npgsql v3.2.2 and EntityFramework6.Npgsql to v3.1.1 logging gives error on:

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

The following error:

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]

web config changes

for 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>

for 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 DB Column definition :

 "ENTRY_DATE" timestamp without time zone

What changes when upgrade to v3.x? Please help to use for right way on v3.x.

Thanks in advance.

c#
postgresql
entity-framework
nlog
asked on Stack Overflow May 20, 2017 by Fatih • edited May 22, 2017 by Julian

1 Answer

2

Updated answer

Since NLog 4.6 it's possible to configure the parameter type with the dbType attribute. NLog 4.6 has been released in March 2019.

E.g.

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

See 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

Original answer:

The parameters from the database target in NLog are text parameters.

It seems that Npgsql/PostgreSQL/EntityFramework6 is now more picky.

You need to convert the string to the timestamp type. You can do that in the query with the to_timestamp PostgreSQL function:

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

The format in C#: yyyy-MM-dd HH:mm:ss.ffff, in PostgreSQL: YYYY-MM-DD H24:MI:SS.MS - see PostgreSQL docs

answered on Stack Overflow May 22, 2017 by Julian • edited Feb 8, 2021 by Julian

User contributions licensed under CC BY-SA 3.0