[英]Why is my Serilog PostgreSQL sink not writing when deployed to AWS Lambda?
I have a .NET Core API that uses Serilog for logging.我有一个使用 Serilog 进行日志记录的 .NET Core API。 When deployed to AWS Lambda, the Console sink works perfectly and writes all messages to AWS CloudWatch .当部署到 AWS Lambda 时,控制台接收器可以完美运行并将所有消息写入 AWS CloudWatch 。
I also have a PostgreSQL sink (Serilog.Sinks.Postgresql.Alternative) which writes to an AWS database.我还有一个写入 AWS 数据库的 PostgreSQL 接收器 (Serilog.Sinks.Postgresql.Alternative)。 This works perfectly from my local dev machine.这在我的本地开发机器上完美运行。 However, when I push it to my AWS Lambda, the database is not getting written to.但是,当我将它推送到我的 AWS Lambda 时,数据库没有被写入。
This is in my Program.Main
.这是在我的Program.Main
中。
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.PostgreSQL(connectionString, TableName, columnWriters)
.CreateLogger();
What am I missing?我错过了什么? Maybe it's not flushing the cache on each HTTP request?也许它没有在每个 HTTP 请求上刷新缓存?
UPDATE: I created a custom .NET Core ILogger, and it has the same behavior.更新:我创建了一个自定义的 .NET Core ILogger,它具有相同的行为。 That is, it works locally, but not when deployed to my Lambda.也就是说,它在本地工作,但在部署到我的 Lambda 时不能。
Depending on how your code is structured, if you have a separate Lambda entry point class, your Program.Main is skipped (it's only called when you run dotnet run
, thus why it works locally).根据您的代码结构,如果您有一个单独的 Lambda 入口点类,则会跳过您的 Program.Main(它仅在您运行dotnet run
时调用,因此它在本地工作)。
You will need to configure the logging in the Lambda entry point class.您将需要在 Lambda 入口点类中配置日志记录。 For example:例如:
public class LambdaEntryPoint : APIGatewayHttpApiV2ProxyFunction
{
protected override void Init(IWebHostBuilder builder)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.PostgreSQL(connectionString, TableName, columnWriters)
.CreateLogger();
builder.UseStartup<Startup>().UseSerilog();
}
}
You might find this also doesn't work, the reason being the logs need to be flushed, but you cannot use CloseAndFlush in a lambda, as that closes the stream and there will be no logs on next invocation.您可能会发现这也不起作用,原因是需要刷新日志,但您不能在 lambda 中使用 CloseAndFlush,因为这会关闭流并且下次调用时不会有日志。 Logs to the Console, however, do not need to be flushed.但是,不需要刷新到控制台的日志。
A solution would be to log to Console, so the logs go to CloudWatch, and then create a Lambda Function triggered by CloudWatch logs for your service, and send them to your PostgreSQL.一种解决方案是登录到控制台,因此日志会转到 CloudWatch,然后为您的服务创建由 CloudWatch 日志触发的 Lambda 函数,并将它们发送到您的 PostgreSQL。 Here is an example of a Node.js Lambda function for forwarding logs to Seq . 下面是一个用于将日志转发到 Seq 的 Node.js Lambda 函数示例。
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.