I’ve defined two helper methods - True
and False
- to apply side effects and then return a boolean. I recommend using False if the body of your catch is nothing more than throw;. When an exception is thrown, the exception filter is run and the exception is logged, and then the false result means that the exception filter does not match the exception, and the runtime continues searching for a matching handler.
Another scenario is if the catch block actually handles the exception. Say, if we know there is an exception that is safe to ignore. In that case, use the True helper method so that the exception matches the catch block and the stack is unwound and the exception is handled there.
Both helpers are useful in different scenarios.
The solution here unfortunately does not work well with async code. This is because async will cause exceptions to be caught and then re-thrown at the point of the await. So, the exception filter runs at the point of the await instead of where the exception was originally thrown.
Tip: The Console logger ignores logging scopes by default; they have to be manually enabled.