0

When running application in Visual Studio there is a setting which allow debugger to break anytime some specified exception is being thrown (even if it's properly caught by the application).

This is a very useful feature because in my application some exceptions (e.g. null reference) should never occur and the programmer should write a code in a way to avoid them (e.g. always perform null-check where null could appear). Thanks to the above mentioned feature my debugger will always break directly at the place of bug when this unwanted exception occur.

Now I would like to inject the similar logic to the release code. I would like every instance of some exception be logged with a stacktrace whenever it occurs even if this exception is caught and handled in the code. Is there such a mechanism in C#?

I could obviously add (catch NullReferenceException) clause to all try/catch blocks, but this would be very tedious.

Kuba
  • 2,986
  • 2
  • 26
  • 32

1 Answers1

1

You want the FirstChanceException event on the AppDomain class

Loofer
  • 6,841
  • 9
  • 61
  • 102
Sean
  • 60,939
  • 11
  • 97
  • 136
  • Also if your thing is an asp.net thing then ELMAH can log things nicely. https://elmah.github.io/ – Loofer Dec 07 '17 at 12:43