title: NLog Raygun - How to catch exception when calling SendInBackground
body: <p>I forked the NLog Raygun from Mindscape to provide more settings of Raygun in NLog.</p>
  
  <p>A RaygunTarget is created and needs to override <code>Write</code> method as follows NLog document (<a href="https://github.com/NLog/NLog/wiki/How-to-write-a-custom-target" rel="nofollow noreferrer">https://github.com/NLog/NLog/wiki/How-to-write-a-custom-target</a>).</p>
  
  <p>To send an exception to Raygun, follows the below code</p>
  
  <pre><code>protected override void Write(LogEventInfo logEvent)
  {
      _raygunClient ??= (_raygunClient = CreateRaygunClient());
      ...
      _raygunClient.SendInBackground(exception, tags, userCustomData);
  }
  </code></pre>
  
  <p><code>SendInBackground</code> returns a Task which we can not mark <code>Write</code> method as <code>async void</code> because it's not the case of using <code>async void</code>. </p>
  
  <p>Everything works fine. We have a swagger to have Healthcheck. I want to check if NLog Raygun is sent successfully but I don't know how to catch the exception in <code>SendInBackground</code> in case API Key is invalid. I mean is there a better way to test whether this RaygunTarget can send a message successfully to Raygun?</p>
  
  <p>If I provide a setting called ThrowOnError and in <code>Write</code> method updated like this</p>
  
  <pre><code>protected override void Write(LogEventInfo logEvent)
  {
      _raygunClient ??= (_raygunClient = CreateRaygunClient());
      ...
      var task = _raygunClient.SendInBackground(exception, tags, userCustomData);
      if (_setting.ThrowOnError) task.Wait();
  }
  </code></pre>
  
  <p>It works but a question is: This is a good way to do that? Personally, I think it's not because it introduces a different behavior or side-effect based on a setting.</p>