Edit2:
2024-09-11T11:49:04.0440649Z Exception while emitting periodic batch from Serilog.Sinks.PostgreSQL.PostgreSQLSink: Npgsql.PostgresException (0x80004005): 22P02: invalid input syntax for type json
I’ve installed following NuGet packages:
<ItemGroup>
<PackageReference Include="Serilog" Version="4.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2" />
<PackageReference Include="Serilog.Expressions" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.PostgreSQL" Version="2.3.0" />
<PackageReference Include="Serilog.Sinks.PostgreSQL.Configuration" Version="1.0.1" />
</ItemGroup>
The problem is direct WriteTo config for PostgreSQL works
"WriteTo": [
{
"Name": "PostgreSQL",
"Args": {
"connectionString": "AppDbConnection",
"tableName": "tblAppError",
"needAutoCreateTable": true
}
}
]
but when used with expressions it is not working
When specified to store for log level Error only, it does not create table in database. I’ve changed condition from Log Level to SourceContext but still not working.
What am I missing here?
In Program.cs file
builder.Host.UseSerilog((context, config) => config.ReadFrom.Configuration(context.Configuration));
appSettings.json
{
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Expressions",
"Serilog.Sinks.PostgreSQL.Configuration"
],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft.AspNetCore.Mvc": "Warning",
"Microsoft.AspNetCore.Routing": "Warning",
"Microsoft.AspNetCore.Hosting": "Warning"
}
},
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "PostgreSQL",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "StartsWith(SourceContext,'SerilogWork.API.Handlers.GlobalExceptionHandler')"
}
}
],
"WriteTo": [
{
"Name": "PostgreSQL",
"Args": {
"connectionString": "AppDbConnection",
"tableName": "tblAppError",
"needAutoCreateTable": true
}
}
]
}
}
}
],
"Enrich": [
"FromLogContext"
]
},
"AllowedHosts": "*",
"ConnectionStrings": {
"AppDbConnection": "Server=localhost;Database=templateDb;User ID=postgres;Password=***;"
},
"Columns": {
"message": "RenderedMessageColumnWriter",
"message_template": "MessageTemplateColumnWriter",
"level": {
"Name": "LevelColumnWriter",
"Args": {
"renderAsText": true,
"dbType": "Varchar"
}
},
"raise_date": "TimestampColumnWriter",
"exception": "ExceptionColumnWriter",
"properties": "LogEventSerializedColumnWriter",
"props_test": {
"Name": "PropertiesColumnWriter",
"Args": {
"dbType": "Varchar"
}
}
}
}
After writing this post I’ve tried with
Note that file logs are getting created while PostgreSQL does not even create table
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "SourceContext = 'SerilogWork.API.Handlers.GlobalExceptionHandler'"
}
}
],
"WriteTo": [
{
"Name": "PostgreSQL",
"Args": {
"connectionString": "AppDbConnection",
"tableName": "tblAppError",
"needAutoCreateTable": true
}
},
{
"Name": "File",
"Args": {
"path": "Logs/log-.txt",
"rollingInterval": "Day",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
"fileSizeLimitBytes": 10485760,
"rollOnFileSizeLimit": true,
"shared": true,
"flushToDiskInterval": 1
}
}
]
}
1