How to send an “exception” to Application Insights in .NET?

I’m using Application Insights in my .NET 5 application. Our application uses custom error handling and I noticed that my exceptions where not visible in the Application Insights dashboard… Even though they were logged by the .NET 5 application.

Wouter
2 min readOct 2, 2021

After Googling around a bit I found out that the ‘problem’ was in the way I was sending my exceptions to Application Insights. The exceptions were logged.. Just not as an exception…

Notice how I have 0 exceptions but tons of requests and traces?

By the way if you haven’t configured Application Insights for your application yet, find out how to do so here.

The solution

When you use logger.LogError you will see your error as a trace in Application Insights. For example:

logger.LogError("$"Object not found with id {objectId}");

Will give you:

If you want this message to appear under the Exception tab in Application Insights you need to pass in an exception object.

// create an exception and store it in a variable.
var ex = new…

--

--