Aws lambda ConsoleLogger在aws lambda 3.1中写入日志时顺序不正确

Aws lambda ConsoleLogger在aws lambda 3.1中写入日志时顺序不正确,aws-lambda,.net-core-3.1,Aws Lambda,.net Core 3.1,我们有一个带有.net core 3.1的AWS lambda,我们使用依赖注入来添加一些服务,其中一个服务是ConsoleLogger,我们像这样注入记录器: private void ConfigureServices(IServiceCollection services) { this.Configuration = new ConfigurationBuilder().AddEnvironmentVariables().Build(); services.AddOpti

我们有一个带有.net core 3.1的AWS lambda,我们使用依赖注入来添加一些服务,其中一个服务是ConsoleLogger,我们像这样注入记录器:

private void ConfigureServices(IServiceCollection services)
{
    this.Configuration = new ConfigurationBuilder().AddEnvironmentVariables().Build();
    services.AddOptions();
    services.AddLogging(builder =>
    {
        builder.AddConsole((x) =>
        {
            x.DisableColors = true;
            x.Format = Microsoft.Extensions.Logging.Console.ConsoleLoggerFormat.Systemd;
        });
    });
    
    // more services
}
[LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
public async Task Handle(ILambdaContext lambdaContext)
{
    var logger = this.ServiceProvider.GetService<ILogger<MyClass>>();

    string startTime = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
    logger.LogInformation($"Start Time stamp:{startTime}|AwsRequestId:{lambdaContext.AwsRequestId}");
    
    // more work
    logger.LogInformation("processing x");
    // more work
    string endTime = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
    logger.LogInformation($"End Time stamp:{endTime}|AwsRequestId:{lambdaContext.AwsRequestId}");
}
然后在函数中,我们使用如下记录器:

private void ConfigureServices(IServiceCollection services)
{
    this.Configuration = new ConfigurationBuilder().AddEnvironmentVariables().Build();
    services.AddOptions();
    services.AddLogging(builder =>
    {
        builder.AddConsole((x) =>
        {
            x.DisableColors = true;
            x.Format = Microsoft.Extensions.Logging.Console.ConsoleLoggerFormat.Systemd;
        });
    });
    
    // more services
}
[LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
public async Task Handle(ILambdaContext lambdaContext)
{
    var logger = this.ServiceProvider.GetService<ILogger<MyClass>>();

    string startTime = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
    logger.LogInformation($"Start Time stamp:{startTime}|AwsRequestId:{lambdaContext.AwsRequestId}");
    
    // more work
    logger.LogInformation("processing x");
    // more work
    string endTime = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
    logger.LogInformation($"End Time stamp:{endTime}|AwsRequestId:{lambdaContext.AwsRequestId}");
}
[LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
公共异步任务句柄(ILambdaContext lambdaContext)
{
var logger=this.ServiceProvider.GetService();
字符串startTime=DateTime.UtcNow.ToString(“yyyy-MM-dd-HH:MM:ss.fff”,CultureInfo.InvariantCulture);
logger.LogInformation($“开始时间戳:{startTime}| AwsRequestId:{lambdaContext.AwsRequestId}”);
//更多工作
logger.LogInformation(“处理x”);
//更多工作
字符串endTime=DateTime.UtcNow.ToString(“yyyy-MM-dd-HH:MM:ss.fff”,CultureInfo.InvariantCulture);
logger.LogInformation($“结束时间戳:{endTime}| AwsRequestId:{lambdaContext.AwsRequestId}”);
}
问题是,在cloudwatch中,日志是无序的

甚至成本报告也在我进入之前

有没有办法避免这种情况


谢谢

不,我不相信你能用CloudWatch做到这一点。CloudWatch保证交付,而不是及时交付。您可以设置Dynamo或ElasticSearch数据库,并将日志消息写入带有时间戳的数据库。检索时,您可以按时间戳排序。与CloudWatch相比,这还可以让您更好地控制邮件过滤。

不,我不相信您可以用CloudWatch做到这一点。CloudWatch保证交付,而不是及时交付。您可以设置Dynamo或ElasticSearch数据库,并将日志消息写入带有时间戳的数据库。检索时,您可以按时间戳排序。这也使您能够比CloudWatch更有效地控制邮件的过滤。

ConsoleLogger
将邮件缓冲在内部队列中,因此它们可能会延迟,而这与CloudWatch无关。亚马逊自己的CloudWatch日志库也做了同样的事情,他们在自己的文档中指出,这可能是Lambdas的一个问题:


他们建议的解决方案是使用不进行任何缓冲的消息。ConsoleLogger将消息缓冲在内部队列中,因此消息可能会在那里延迟,这与CloudWatch无关。亚马逊自己的CloudWatch日志库也做了同样的事情,他们在自己的文档中指出,这可能是Lambdas的一个问题:

他们推荐的解决方案是使用不做任何缓冲的