简体   繁体   English

为什么我的 Serilog PostgreSQL 接收器在部署到 AWS Lambda 时不写入?

[英]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 时,数据库没有被写入。

  • I'm using the exact same database and connection string.我正在使用完全相同的数据库和连接字符串。
  • I'm using the exact same DB user.我正在使用完全相同的数据库用户。
  • I can successfully write to the DB from my DAL--apart from logging.除了日志记录之外,我可以从我的 DAL 成功写入数据库。

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.

相关问题 为什么NextJS中的此功能在本地工作,而在AWS Lambda中部署却不起作用? - Why does this function in NextJS works locally, but not when deployed in AWS Lambda? 使用 CDK 部署时 AWS Lambda 无法正常工作 - AWS Lambda not working when deployed with CDK 从我的 IAM 部署的 AWS lambda 函数列表 - List of AWS lambda functions deployed from my IAM Lambda在本地连接到Aurora MySql-部署到AWS时超时 - Lambda connects to Aurora MySql locally - times out when deployed to AWS 在AWS Lambda中部署后如何处理Django应用中的会话 - How to handle sessions in django app when deployed in aws lambda Webpack 4 + Jest + Babel 7 (+AWS Lambda):本地测试通过,部署到 AWS Lambda 时出错 - Webpack 4 + Jest + Babel 7 (+AWS Lambda): tests pass locally, error when deployed to AWS Lambda 使用 postgresql 调用 AWS Lambda - Invoking AWS Lambda with postgresql 使用 AWS lambda 将自定义指标数据写入 CloudWatch 时超时 - Timeout when writing custom metric data to CloudWatch with AWS lambda 为什么我的 Lambda function 有时只写入我的 DynamoDB 表? - Why is my Lambda function only sometimes writing to my DynamoDB table? 为什么无法访问部署在AWS(免费)上的Bitnami WordPress网站? - Why is my Bitnami WordPress website deployed on AWS (free tier) not accessible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM