title: .Net Core AWS Lambda function not flushing logs
body: <p>I have set up a .NET Core AWS Lambda function using the AWS Toolkit Extensions's template project structure. The function works as expected. The only issue I have is that my logs are not always flushed before the application shuts down. I usually lose about one or two lines of logs. I am using AWS Cloudwatch to store my logs and hooking into the inbuilt .NET Core logging framework.</p>
  
  <p>I believe the issue is that the logger pushes it's logs in batches rather than immediately sending them. The problem is that my Lambda is obviously finishing before the logger can send it's last batch, so some get missed.</p>
  
  <p>I've tried reducing the batch push interval to one second but I'm still having issues. I also tried disposing the service provider I'm using in the hope that this would force the logger to flush it's logs but that doesn't seem to have helped either.</p>
  
  <p>I've been unable to find a way to manually flush the logs from the .NET Core logger.</p>
  
  <p>I don't really want to be adding anything like <code>Thread.Sleep</code> to the end of my function - it's messy and I really shouldn't have to do this.</p>
  
  <p>Here is my code:</p>
  
  <p><strong>Function entry:</strong></p>
  
  <pre><code>public class Function
  {
      private readonly Startup startup;
  
      private ServiceProvider serviceProvider;
      private ILogger&lt;Function&gt; logger;
  
      public Function()
      {
          startup = new Startup();
      }
  
      public async Task FunctionHandler(SendUnsentBrochuresRequest request, ILambdaContext context)
      {
          serviceProvider = startup.InitializeFunction();
          logger = this.serviceProvider.GetService&lt;ILogger&lt;Function&gt;&gt;();
  
          try
          {
              this.logger.LogInformation("Beginning function...");
              var service = this.serviceProvider.GetService&lt;SendUnsentBrochuresService&gt;();
              await service.Process(request);
          }
          catch (Exception ex)
          {
              this.logger.LogError(ex, ex.FullExceptionDetails());
          }
          finally
          {
              this.logger.LogInformation("Finished sending unsent brochure requests.");
              this.serviceProvider.Dispose();
          }
      }
  }
  </code></pre>
  
  <p><strong>Startup:</strong>
  (Using <code>AWS.Logger.AspNetCore</code> nuget package)</p>
  
  <pre><code>public class Startup
  {
      public ServiceProvider InitializeFunction()
      {
          var services = new ServiceCollection();
          var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
  
          // Config
          var configBuilder = new ConfigurationBuilder()
              .SetBasePath(Directory.GetCurrentDirectory())
              .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
              .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
              .AddEnvironmentVariables();
  
          var config = configBuilder.Build();
          services.AddSingleton&lt;IConfiguration&gt;(config);
  
          var awsCredentials = new BasicAWSCredentials(config["AWS:AccessKey"], config["AWS:SecretKey"]);
  
          // Logging
          services.AddLogging(builder =&gt;
          {
              var options = new AWSLoggerConfig()
              {
                  Credentials = awsCredentials,
                  LogGroup = config["AWS:Cloudwatch:LogGroup"],
                  Region = "eu-west-2",
                  BatchPushInterval = TimeSpan.FromSeconds(1)
              };
  
              builder.AddFilter("Microsoft", LogLevel.Warning);
  
              builder.AddAWSProvider(options);
          });
  
          // Other stuff...
  
          // Function
          services.AddTransient&lt;SendUnsentBrochuresService&gt;();
  
          return services.BuildServiceProvider();
      }
  }
  </code></pre>