.Net Core日志记录到PostgreSQL,Serilog不起作用



我正在使用Serilog来记录Postgresql数据库这是我的序列号配置

  1. 这是我的appsettings.json->{还尝试了不同的配置}
"Serilog": {
"Using": [ "Serilog.Sinks.PostgreSQL.Configuration" ],
"MinimumLevel": "Debug",
"Enrich": [ "WithMachineName" ],
"WriteTo": [
{
"Name": "PostgreSQL",
"Args": {
"connectionString": "my-connection-string",
"tableName": "log",
"needAutoCreateTable": true
}
}
]
},
"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": "Json" }
},
"machine_name": {
"Name": "SinglePropertyColumnWriter",
"Args": {
"propertyName": "MachineName",
"writeMethod": "Raw"
}
}
},
  1. 这是程序.cs
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
//Initialize Logger
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(config)
.CreateLogger();
try
{
Log.Information("Application Starting.");
CreateHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "The Application failed to start.");
}
finally
{
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
  1. 这是控制器
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace MyApp
{
[ApiController]
public class AuthenticationController : ControllerBase
{
private readonly IAuthenticationService _authenticationService;
private readonly ILogger<AuthenticationController> _logger;
public AuthenticationController(
IAuthenticationService authenticationService,
ILogger<AuthenticationController> logger)
{
this._authenticationService = authenticationService;
this._logger = logger;
}
[Route("test-log")]
[HttpGet]
public IActionResult LOG()
{
_logger.LogInformation("Hello World");
return Ok();
}
}
}

启动后,表已创建,但没有记录。还测试了Serilog日志是否比微软有同样的问题。

遗憾的是,Serolog.Sinks.PostgreSQL包尚未更新以支持Npgsql5.0。事实上,他们仍在尝试以.NET Framework为目标,但Npgsql5.0只支持.NET Core。

Github上有一个问题:https://github.com/b00ted/serilog-sinks-postgresql/issues/34

顺便说一下,您可以通过将以下内容添加到启动配置中来调试Serilog问题:

SelfLog.Enable(msg =>
{
Debug.Print(msg);
Debugger.Break();
});

尽管我通过Program.cs.中的.WriteTo.PostgreSQL()在Program.cs中配置它,但我也面临着类似的情况

在我的案例中,解决方案是将参数'useCopy'设置为false

示例:

...
.WriteTo.PostgreSQL(
connectionString: configuration.GetConnectionString("LoggerConnection").ToString(),
tableName: "yourtable",
columnOptions: columnWriters,
restrictedToMinimumLevel: LogEventLevel.Information,
needAutoCreateTable: true,
respectCase: true,
useCopy: false // <-----------------
)
...

也许您可以在appsettings.json配置中尝试做同样的操作。

最新更新