title: Stop Exception generated by CancellationToken from getting Reported by ApplicationInsights
body: <p>I <em>thought</em> it would be a good idea to use <code>CancellationToken</code> in my controller like so:</p>
  <pre><code>[HttpGet(&quot;things&quot;, Name = &quot;GetSomething&quot;)]
  public async Task&lt;ActionResult&gt; GetSomethingAsync(CancellationToken ct) {
      var result = await someSvc.LoadSomethingAsync(ct);
      return Ok(result);
  }
  </code></pre>
  <p>The issue is that now Azure Application Insights shows a bunch of exceptions of type <code>System.Threading.Tasks.TaskCancelledException: A Task was canceled.</code> as well as  the occasional <code>Npgsql.PostgresException: 57014: canceling statement due to user request</code>.  This is noise that I don't need.</p>
  <p>Application Insights is registered as a service using the standard method - <code>services.AddApplicationInsightsTelemetry(Configuration);</code>.</p>
  <h3>Attempted Solution</h3>
  <p>I thought I could jack into the application pipeline and catch the above Exceptions, converting them to normal Responses.  I put the code below in various places.  It catches any exceptions and if IsCancellationRequested, it returns a dummy response.  Otherwise it rethrows the caught exception.</p>
  <pre><code>app.Use(async (ctx, next) =&gt;
  {
      try { await next(); }
      catch (Exception ex)
      {
          if (ctx.RequestAborted.IsCancellationRequested)
          {       
              ctx.Response.StatusCode = StatusCodes.Status418ImATeapot;
          }
          else { throw; }
      }
  });
  </code></pre>
  <p>This code works in that it changes the response.  However exceptions are still getting sent to Application Insights.</p>
  <h3>Requirements</h3>
  <ul>
  <li>I would like to have a solution that uses <code>RequestAborted.IsCancellationRequested</code> over trying to catch specific exceptions.  The reason being that if I've already discovered one implementation that throws an exception not derived from <code>OperationCanceledException</code> the possibility exists there are others that will do the same.</li>
  <li>It doesn't matter if dependency failures still get logged, just that the exceptions thrown as a result of the request getting canceled don't.</li>
  <li>I don't want to have a try/catch in every controller method.  It needs to be put in place in one spot.</li>
  </ul>
  <h3>Conclusion</h3>
  <p>I wish I understood Application Insights mechanism of reporting exceptions.  After experimenting I feel like trying to catch errors in the Application pipeline isn't the correct approach.  I just don't know what is.</p>