AWS Lambda & ASP.NET Core Logging

Viewed 3610

I have a ASP.NET Core 3.1 running in AWS Lambda with following setup:


  <ItemGroup>
    <PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="5.1.1" />
    <PackageReference Include="Amazon.Lambda.Logging.AspNetCore" Version="3.0.1" />
    <PackageReference Include="AutoMapper" Version="10.0.0" />
    <PackageReference Include="AutoMapper.Collection" Version="7.0.0" />
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.0.1" />
    <PackageReference Include="AWS.Logger.AspNetCore" Version="2.2.0" />
    
    <PackageReference Include="AWSSDK.DynamoDBv2" Version="3.3.105.26" />
    <PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.3.101" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
    ...

LambdaEntryPoint.cs:

    public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
    {
        protected override void Init(IWebHostBuilder builder)
        {
            builder
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    var env = hostingContext.HostingEnvironment;

                    config
                        .SetBasePath(env.ContentRootPath)
                        .AddJsonFile("appsettings.json", optional: true)
                        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

                    config.AddEnvironmentVariables();
                })
                
                .ConfigureLogging(logging =>
                {
                    logging.AddAWSProvider();
        
                    // // When you need logging below set the minimum level. Otherwise the logging framework will default to Informational for external providers.
                    // logging.SetMinimumLevel(LogLevel.Debug);
                })

                .UseStartup<Startup>()
            ;
        }
    }

appSettings.json

{
  "Logging": {
    "IncludeLogLevel": true,
    "IncludeCategory": true,
    "IncludeNewline": true,
    "IncludeException": true,
    "IncludeEventId": false,
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Information",
      "System": "Warning",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Warning"
    }
  },
  
  "AllowedHosts": "*"
}

Final result in AWS CloudWatch log looks like this: enter image description here

There are invalid characters [40m[32minfo[39m[22m[49m: and also redundant lines. This is just a sample, but in case of exceptions with stack trace, logs are getting pretty long and are hard to read.

I am simply using Microsoft's ASP.NET Core default logger interface ILogger<MySampleController> and then calling

this.logger.LogInformation($"Mapping done, took {sw.ElapsedMilliseconds} ms.");

What I am doing wrong? How to get log properly configured for AWS Cloudwatch?

Thanks!

1 Answers
Related