I thought it would be a good idea to use CancellationToken
in my controller like so:
[HttpGet("things", Name = "GetSomething")]
public async Task<ActionResult> GetSomethingAsync(CancellationToken ct) {
var result = await someSvc.LoadSomethingAsync(ct);
return Ok(result);
}
The issue is that now Azure Application Insights shows a bunch of exceptions of type System.Threading.Tasks.TaskCancelledException: A Task was canceled.
as well as the occasional Npgsql.PostgresException: 57014: canceling statement due to user request
. This is noise that I don't need.
Application Insights is registered as a service using the standard method - services.AddApplicationInsightsTelemetry(Configuration);
.
Attempted Solution
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.
app.Use(async (ctx, next) =>
{
try { await next(); }
catch (Exception ex)
{
if (ctx.RequestAborted.IsCancellationRequested)
{
ctx.Response.StatusCode = StatusCodes.Status418ImATeapot;
}
else { throw; }
}
});
This code works in that it changes the response. However exceptions are still getting sent to Application Insights.
Requirements
- I would like to have a solution that uses
RequestAborted.IsCancellationRequested
over trying to catch specific exceptions. The reason being that if I've already discovered one implementation that throws an exception not derived fromOperationCanceledException
the possibility exists there are others that will do the same. - 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.
- I don't want to have a try/catch in every controller method. It needs to be put in place in one spot.
Conclusion
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.