[英].Net Core Logging to PostgreSQL with Serilog Not Working
我正在使用 Serilog 记录 Postgresql 数据库这是我的 Serilog 配置
"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"
}
}
},
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>();
});
}
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 日志记录是否比 Microsoft 但遇到了同样的问题。
不幸的是,Serilog.Sinks.PostgreSQL package 尚未更新以支持 Npgsql 5.0。 实际上他们仍在尝试针对 .NET 框架,但 Npgsql 5.0 仅支持 .NET 核心。
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 配置中做同样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.