gpt4 book ai didi

NLog,Npgsql.PostgresException : '42804: column "DateTime"is of type timestamp without time zone but expression is of type text

转载 作者:行者123 更新时间:2023-12-02 20:26:22 25 4
gpt4 key购买 nike

我使用 NLog 登录到 PostrgeSQL 数据库。

我创建了表格:

CREATE TABLE "Data"."Logs"
(
"ID" serial primary key,
"Level" character varying(20),
"DateTime" timestamp,
"Message" character varying,
"Exception" character varying
)

然后我配置了目标:

var logDB = new DatabaseTarget()
{
Name = "logDB",
DBProvider = "Npgsql.NpgsqlConnection,Npgsql",
ConnectionString = connectionString,
KeepConnection = true,
CommandText = $@"INSERT INTO ""{SchemaName}"".""{FileManagerLogsTableName}""(""{Level}"", ""{DateTime}"", ""{Message}"", ""{Exception}"") VALUES (@level, @datetime, @message, @exception)"
};

logDB.Parameters.Add(new DatabaseParameterInfo("@level", "${level}"));
logDB.Parameters.Add(new DatabaseParameterInfo("@datetime", "${date:format=yyyy-MM-dd hh:mm:ss}"));
logDB.Parameters.Add(new DatabaseParameterInfo("@message", "${message}"));
logDB.Parameters.Add(new DatabaseParameterInfo("@exception", "${exception:format=shortType,message :separator= - }${newline}${exception:format=method}${newline}${exception:format=stackTrace:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}"));

config.LoggingRules.Add(new NLog.Config.LoggingRule("*", LogLevel.Info, logDB));
LogManager.Configuration = config;
LogManager.ThrowExceptions = Debugger.IsAttached;

但是当我想记录时:

logger.Error(ex, ex.Message);

它抛出一个异常:

Npgsql.PostgresException: '42804: column "DateTime" is of type timestamp without time zone but expression is of type text

最佳答案

这是非常简单的解决方案。只需将 DateTime 转换为 CommandText 中的时间戳:

CommandText = $@"INSERT INTO ""{SchemaName}"".""{FileManagerLogsTableName}""(""{Level}"", ""{DateTime}"", ""{Message}"", ""{Exception}"") VALUES (@level, CAST(@datetime AS timestamp), @message, @exception)"

关于NLog,Npgsql.PostgresException : '42804: column "DateTime"is of type timestamp without time zone but expression is of type text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49818430/

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