title: How to log asynchronously with log4net in an ASP.NET MVC Web Application without using up all available threads?
body: <p>Is <a href="https://github.com/cjbhaines/Log4Net.Async" rel="nofollow noreferrer"><code>AsyncForwardingAppender</code> of the <code>Log4Net.Async</code> package</a> safe to use in an ASP.NET MVC Web Application? I'm worried that it will clog up the available threads.</p>
  
  <p>It comes down to me wanting to make a call to an <code>async</code> method to send the logs to an HTTP API. I could use an <code>async void</code> method like <a href="http://blogs.microsoft.co.il/dorony/2013/10/16/the-case-of-the-async-log4net-appender/" rel="nofollow noreferrer">the way this guy did it</a>:</p>
  
  <pre><code>protected override async void Append(log4net.Core.LoggingEvent loggingEvent)
  {
      var message = new SplunkMessage(loggingEvent.Level, loggingEvent.ExceptionObject);
      var success = await _splunkLogger.Report(message);
      //do something with the success status, not really relevant
  }
  </code></pre>
  
  <p>He later <a href="http://blogs.microsoft.co.il/dorony/2013/10/30/async-void-methods-can-crash-your-process/" rel="nofollow noreferrer">updated his code</a>:</p>
  
  <pre><code>public void DoAppend(log4net.Core.LoggingEvent loggingEvent)
  {
      var clientIp = _clientIpRetriever.GetClientIp();
      var message = new SplunkMessage(loggingEvent.Level, loggingEvent.ExceptionObject, clientIp);
      SendMessageToSplunk(message);
  }
  
  private async void SendMessageToSplunk(SplunkMessage message)
  {
      try
      {
          var success = await _splunkLogger.Report(message);
          //do something unimportant
      }
      catch(Exception x)
      {
          LogLog.Error(GetType(), "Error in SplunkAppender.", x);
      }            
  }
  </code></pre>
  
  <p>But I'm too scared to actually try it because of the <a href="https://stackoverflow.com/a/18509424/552379">dangers involved</a>: "First off, let me point out that "fire and forget" is almost always a mistake in ASP.NET applications".</p>
  
  <p>Any suggestions?</p>