title: AWS Lambda & ASP.NET Core Logging
body: <p>I have a ASP.NET Core 3.1 running in AWS Lambda with following setup:</p>
  <pre><code>
    &lt;ItemGroup&gt;
      &lt;PackageReference Include=&quot;Amazon.Lambda.AspNetCoreServer&quot; Version=&quot;5.1.1&quot; /&gt;
      &lt;PackageReference Include=&quot;Amazon.Lambda.Logging.AspNetCore&quot; Version=&quot;3.0.1&quot; /&gt;
      &lt;PackageReference Include=&quot;AutoMapper&quot; Version=&quot;10.0.0&quot; /&gt;
      &lt;PackageReference Include=&quot;AutoMapper.Collection&quot; Version=&quot;7.0.0&quot; /&gt;
      &lt;PackageReference Include=&quot;AutoMapper.Extensions.Microsoft.DependencyInjection&quot; Version=&quot;8.0.1&quot; /&gt;
      &lt;PackageReference Include=&quot;AWS.Logger.AspNetCore&quot; Version=&quot;2.2.0&quot; /&gt;
      
      &lt;PackageReference Include=&quot;AWSSDK.DynamoDBv2&quot; Version=&quot;3.3.105.26&quot; /&gt;
      &lt;PackageReference Include=&quot;AWSSDK.Extensions.NETCore.Setup&quot; Version=&quot;3.3.101&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.AspNetCore.Mvc.NewtonsoftJson&quot; Version=&quot;3.1.4&quot; /&gt;
      ...
  </code></pre>
  <p>LambdaEntryPoint.cs:</p>
  <pre><code>    public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
      {
          protected override void Init(IWebHostBuilder builder)
          {
              builder
                  .ConfigureAppConfiguration((hostingContext, config) =&gt;
                  {
                      var env = hostingContext.HostingEnvironment;
  
                      config
                          .SetBasePath(env.ContentRootPath)
                          .AddJsonFile(&quot;appsettings.json&quot;, optional: true)
                          .AddJsonFile($&quot;appsettings.{env.EnvironmentName}.json&quot;, optional: true);
  
                      config.AddEnvironmentVariables();
                  })
                  
                  .ConfigureLogging(logging =&gt;
                  {
                      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&lt;Startup&gt;()
              ;
          }
      }
  </code></pre>
  <p>appSettings.json</p>
  <pre><code>{
    &quot;Logging&quot;: {
      &quot;IncludeLogLevel&quot;: true,
      &quot;IncludeCategory&quot;: true,
      &quot;IncludeNewline&quot;: true,
      &quot;IncludeException&quot;: true,
      &quot;IncludeEventId&quot;: false,
      &quot;IncludeScopes&quot;: false,
      &quot;LogLevel&quot;: {
        &quot;Default&quot;: &quot;Information&quot;,
        &quot;System&quot;: &quot;Warning&quot;,
        &quot;Microsoft&quot;: &quot;Warning&quot;,
        &quot;Microsoft.Hosting.Lifetime&quot;: &quot;Warning&quot;
      }
    },
    
    &quot;AllowedHosts&quot;: &quot;*&quot;
  }
  </code></pre>
  <p>Final result in AWS CloudWatch log looks like this:
  <a href="https://i.stack.imgur.com/k22Xx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k22Xx.png" alt="enter image description here" /></a></p>
  <p>There are invalid characters <code>[40m[32minfo[39m[22m[49m: </code> 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.</p>
  <p>I am simply using Microsoft's ASP.NET Core default logger interface <code>ILogger&lt;MySampleController&gt;</code> and then calling</p>
  <pre><code>this.logger.LogInformation($&quot;Mapping done, took {sw.ElapsedMilliseconds} ms.&quot;);
  </code></pre>
  <p>What I am doing wrong? How to get log properly configured for AWS Cloudwatch?</p>
  <p>Thanks!</p>