我已经通过代码为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()ProjectFile
<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>Config文件
{
"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!");
}配置文件与描述的这里完全相同。为了测试目的,我尝试了使用.net 5.0和3.1,但没有成功。
不幸的是,我不能通过代码使用它,因为我使用的是一个第三方库,它使用serilog登录到sql server,而且我只能通过配置切换到PostgrSQL。有什么帮助吗?
发布于 2021-03-16 19:14:02
之所以会出现此错误,是因为Serilog.Sinks.PostgreSQL依赖于Npgsql v4.0.2,但需要将Npgsql升级到v5.0.3,这与Serilog.Sinks.PostgreSQL的最新版本不兼容(本文编写时的v2.2.0)。
<PackageReference Include="Npgsql" Version="5.0.3" />这是Npgsql中的二进制中断更改,这会导致MissingMethodException,因为..。在4.x中预期的方法在5.x中不再存在。
对于您来说,一个直接的解决方案是删除Npgsql包引用并使用v4.2.0,它将由Serilog.Sinks.PostgreSQL临时添加。或者,您可以将Npgsql升级到最新的4.x版本,这是本文编写时的v4.1.8版本。
长期的解决方案是在Serilog.Sinks.PostgreSQL的回购中打开一个问题,这样他们就可以发布一个依赖于Npgsql 5.x的新版本。
Update:看起来Serilog.Sinks.PostgreSQL不再被维护,这里有一个分支:https://github.com/SeppPenner/SerilogSinkForPostgreSQL,它是最新的
https://stackoverflow.com/questions/66658664
复制相似问题