First chance exceptions - KirillOsenkov/Bliki GitHub Wiki

.NET runtime (both Framework and Core) has an event: AppDomain.CurrentDomain.FirstChanceException: https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.firstchanceexception?view=netframework-4.8

It is raised any time an exception is thrown, even if there's a try/catch block. It is similar to the exception filters, and is executed early, before the stack is unwound.

Read more details in this Twitter thread: https://twitter.com/KirillOsenkov/status/1469388468824330241

The problem we have is that a lot of first-chance exceptions are caught and not logged anywhere. We don't send telemetry, we don't log, and so a feature doesn't work but we don't know why. Sometimes we log but we don't look in the logs, or the customer doesn't send us the logs, or the logs contain so much noise that exceptions remain unnoticed.

Additionally, due to the debugger not having good support for ignoring benign first-chance exceptions, it is difficult to debug your code with first-chance exceptions on, due to too much noise. We miss a lot of exceptions that could have been easily caught.

  1. Have a list of allowed/benign first-chance exceptions that we should ignore (OperationCanceledException etc). Have an extension point that allows an extension to add a custom exception to the allowed/ignored list.
  2. Send telemetry about all other first-chance exceptions including the prettified call stack (perhaps use https://github.com/benaadams/Ben.Demystifier)
  3. Bucketize all first-chance exceptions on the backend by call stack etc. Add exceptions to allowed list for benign ones that we want to ignore.
  4. Add better debugger support for ignoring classes of first-chance exceptions, based on exception type, message or location (module+function+IL offset)

This would give us great new insights about the hidden defects that we are currently unaware of and only observe after a long investigation into bugs reported by customers.