gpt4 book ai didi

.net - 为什么 Serilog PostgreSQL 接收器不能通过配置工作?

转载 作者:行者123 更新时间:2023-12-04 07:56:52 28 4
gpt4 key购买 nike

我已经通过代码为 Serilog 尝试了 PostgreSQL 接收器,它的工作原理很吸引人。
这是工作代码:

    static void Main(string[] args)
{
var connectionString = "Server=localhost; Port=5432; Database=subscriptions_log; User Id=postgres;Password=postgres";
string tableName = "errorlog";

IDictionary<string, ColumnWriterBase> columnWriters = new Dictionary<string, ColumnWriterBase>
{
{"message", new RenderedMessageColumnWriter(NpgsqlDbType.Text) },
{"level", new LevelColumnWriter(true, NpgsqlDbType.Varchar) },
{"raise_date", new TimestampColumnWriter(NpgsqlDbType.Timestamp) },
{"exception", new ExceptionColumnWriter(NpgsqlDbType.Text) }
};

using var log = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.PostgreSQL(connectionString, tableName, columnWriters)
.CreateLogger();

log.Information("Hello, Serilog!");
}
但是当我通过配置文件 serlig 使用它时,会在数据库中创建表,但由于未找到“Npgsql.NpgsqlBinaryImporter.Complete()”而没有插入任何行。
“这是详细的错误:
Exception while emitting periodic batch from Serilog.Sinks.PostgreSQL.PostgreSQLSink:
System.MissingMethodException:
Method not found: 'Void Npgsql.NpgsqlBinaryImporter.Complete()'.
at Serilog.Sinks.PostgreSQL.PostgreSQLSink.ProcessEventsByCopyCommand(IEnumerable`1 events, NpgsqlConnection connection)
at Serilog.Sinks.PostgreSQL.PostgreSQLSink.EmitBatch(IEnumerable`1 events)
at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatchAsync(IEnumerable`1 events)
at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.OnTick()
项目文件
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Npgsql" Version="5.0.3" />
<PackageReference Include="Serilog" Version="2.10.1-dev-01285" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.2.0-dev-00264" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0-dev-00839" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0-dev-00909" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.3.0" />
<PackageReference Include="Serilog.Sinks.PostgreSQL" Version="2.2.0" />
<PackageReference Include="Serilog.Sinks.PostgreSQL.Configuration" Version="1.0.0" />
</ItemGroup>

</Project>
配置文件
        {
"Serilog": {
"Using": [ "Serilog.Sinks.PostgreSQL.Configuration" ],
"MinimumLevel": "Debug",
"Enrich": [ "WithMachineName" ],
"WriteTo": [
{
"Name": "PostgreSQL",
"Args": {
"connectionString": "LogsDb",
"tableName": "errorlogs",
"needAutoCreateTable": true
}
}
]
},
"ConnectionStrings": {
"LogsDb": "Server=localhost; Port=5432; Database=subscriptions_log; User Id=postgres;Password=postgres"
},
"Columns": {
"message": "RenderedMessageColumnWriter",
"level": {
"Name": "LevelColumnWriter",
"Args": {
"renderAsText": true,
"dbType": "Varchar"
}
},
"raise_date": "TimestampColumnWriter",
"exception": "ExceptionColumnWriter"
}
}
以及调用方法:
    static void Main(string[] args)
{
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
Serilog.Debugging.SelfLog.Enable(Console.Error);

IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..")))
.AddJsonFile("serilog.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

using var log = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();

log.Error("Hello, Serilog!");
}
配置文件与描述完全一致 here .出于测试目的,我尝试了 .net 5.0 和 3.1,但没有成功。
不幸的是,我无法通过代码使用它,因为我使用的是使用 serilog 登录到 sql server 的 3rd 方库,我只能通过配置切换到 PostgrSQL。有什么帮助吗?

最佳答案

您收到此错误的原因是 Serilog.Sinks.PostgreSQL取决于 Npgsql v4.0.2 ,但您强制将 Npgsql 升级到 v5.0.3它与最新版本的 Serilog.Sinks.PostgreSQL 不兼容(在撰写本文时 v2.2.0)。

<PackageReference Include="Npgsql" Version="5.0.3" />
  • In version 4.x the return type of the method NpgsqlBinaryImporter.Complete is void
  • In version 5.x the return type of the method NpgsqlBinaryImporter.Complete is ulong

  • 这是 Npgsql 中的二进制中断更改,导致 MissingMethodException因为... 4.x 中预期的方法在 5.x 中不再存在。
    您的直接解决方案是删除 Npgsql包引用并使用 v4.2.0,它将由 Serilog.Sinks.PostgreSQL 传递添加。或者,您可以将 Npgsql 升级到最新的 4.x 版本,即 v4.1.8在撰写本文时。
    长期的解决方案是在 Serilog.Sinks.PostgreSQL 的 repo 中打开一个问题,以便他们可以发布依赖于 Npgsql 5.x 的新版本。
    更新 : 看起来像 Serilog.Sinks.PostgreSQL不再维护,这里有一个可用的分支: https://github.com/SeppPenner/SerilogSinkForPostgreSQL这是最新的

    关于.net - 为什么 Serilog PostgreSQL 接收器不能通过配置工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66658664/

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