title: IHostedService is not working perfectly some lines of code not firing how to handle it?
body: <p>I am using IHostedService to manage bulk of log inserting. It will run as a background service. But It is not keep going on after for loop  in AsyncConsumer method. How can I use IHostedSErvice as a Background worker permonently always cheking any logs in logs list and consume it.Thank for your help.(Please look at below picture)</p>
  
  <pre><code>     public class QuickLog
      {
          private static BlockingCollection&lt;SimpleLog&gt; data = new BlockingCollection&lt;SimpleLog&gt;();
          public static async Task Producer(SimpleLog pageviewLog)
          {
  
              await Task.Run(() =&gt; data.TryAdd(pageviewLog, TimeSpan.FromSeconds(10)));
          }
  
          public static async Task AsyncConsumer()
          {
              var _client = ElasticConfig.GetClient();
              var logs = new List&lt;SimpleLog&gt;();
               foreach (var item in data.GetConsumingEnumerable())
                  logs.Add(item);
  (After FOR LOOP BELOW CODE NEVER RUN!!!!)
              var response = await _client.IndexManyAsync(logs, logs.FirstOrDefault().IndexName, logs.FirstOrDefault().IndexType);
              if (!response.IsValid)
                  throw response.OriginalException;
  
          }
      }
  </code></pre>
  
  <p>BackgroundService.cs:</p>
  
  <pre><code>
    public class LogBackgroundService : IHostedService
      {
          public  async Task StartAsync(CancellationToken cancellationToken)
          {
              while (!cancellationToken.IsCancellationRequested)
              {
                  await  QuickLog.AsyncConsumer();
                  await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken);
              }
  
          }
  
          public Task StopAsync(CancellationToken cancellationToken)
          {
              return Task.CompletedTask;
          }
      }
  </code></pre>
  
  <p>Startup.cs:</p>
  
  <pre><code> services.AddHostedService&lt;LogBackgroundService&gt;();
  </code></pre>
  
  <p>Program.cs:</p>
  
  <pre><code>
     public class Program
      {
          public static void Main(string[] args)
          {
              BuildWebHost(args).Run();
  
              var hostBuilder = new HostBuilder().ConfigureServices(services =&gt;services.AddHostedService&lt;LogBackgroundService&gt;());
  
  
          }
  
          public static IWebHost BuildWebHost(string[] args) =&gt;
              WebHost.CreateDefaultBuilder(args)
                  .UseApplicationInsights()
                  .UseStartup&lt;Startup&gt;()
                  .Build();
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/KWSzS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KWSzS.png" alt="enter image description here"></a></p>