【发布时间】:2021-07-21 04:33:20
【问题描述】:
我正在使用 Serilog 记录 Postgresql 数据库 这是我的 Serilog 配置
- 这是我的 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"
}
}
},
- 这是 Program.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>();
});
}
- 这里是控制器
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 但遇到了同样的问题。
【问题讨论】:
标签: c# .net postgresql .net-core serilog