Reporting Exceptions in ASP.NET
The secret to my modest success has been mastering the debugging tools. I step through every line of new code I write in a debugger. When I was writing Win32 applications, I memorized dozens of windbg commands, and I used BoundsChecker and Application Verifier. I love inspecting crash dumps. It’s weird, I know, but I enjoy it and employers find it valuable.
My technology stack has changed to ASP.NET applications running in Docker containers, so my debugging tools have change accordingly. It’s almost never possible to step through code running in a live, production ASP.NET application, so a different set of debugging tools is necessary.
The first problem I needed to tackle when I began writing ASP.NET applications was how to debug uncaught exceptions.
Catching Uncaught Exceptions in ASP.NET
By default, when an exception is thrown and not caught in an ASP.NET application, the user sees a page like this.
As a developer, when I see this error message, it’s very tempting to switch the ASPNETCORE_ENVIRONMENT
flag to Development
and then redeploy the app, just for a few minutes so I can see the stack trace, and then redeploy the app again in Production
mode.
But temporarily redeploying an application in Development
mode is a bad idea for at least 2 reasons:
- A
Development
mode application will show malevolent users your stack trace too. They can use that information to hack you. The risk is real. - You’ll never see errors that users did not report to you. Few users go through the hassle of actually reporting errors. More likely, they’ll just stop using your web app, and you’ll never know why.
A Better Way
There are better ways to see the stack trace, and they’re simple to use. My favorite is Stackdriver Error Reporting. Stackdriver Error Reporting can show you the stack trace for every exception your application doesn’t catch.
When an exception is thrown, Stackdriver Error Reporting renders it like this:
As shown above, Stackdriver Error Reporting displays a graph showing how often this error has occurred, and it shows the stack trace of the error, as I circled in red. In this case, the stack trace is only one level deep because controller code is dead simple. How simple? I click on the blue link in the stack trace to see the source:
Looking at the code, no wonder an exception was thrown!
Integrating Stackdriver Error Reporting with an ASP.NET core application
Integrating Stackdriver Error Reporting with an ASP.NET core application requires adding one nuget package:
and adding 8 lines of code to Startup.cs
:
And that’s it! Now my application will report bugs to Stackdriver Error Reporting wherever it’s running.
Stackdriver
Stackdriver is a suite of Application Performance Management (APM) tools that work with 7 programming languages. Stackdriver’s tools work where ever the application is running: on AWS, Azure, Google Cloud Platform, or a corporate network. Stackdriver Error Reporting is the simplest tool in the suite, and honestly, I can’t imagine developing a Web App without it.
Complete documentation for Stackdriver Error Reporting lives here.