Getting started with Application Insights in .NET Web API
Tags: csharp, azure, appinsights, api
Published: 2023-08-20 05:18:26

**Monitoring** an application is one of the most important part in Software Lifecycle. With help of Monitoring tools, we can see whats going inside the application, predict any infra scaling, see how users are using the application etc.
**Azure Application Insights** offers powerful application performance and monitoring tools. It tracks user interactions, traces requests, measures performance metrics, and monitors application health. With features like custom event tracking and real-time analytics, it helps developers identify issues, optimize performance, and enhance user experiences in cloud-based applications.

Pre-Requisites:

1. Azure Account
2. .NET Web API project
3. Visual Studio

## Configuring Application Insights in Web API project

1. Open the solution in Visual Studio
2. Right click on the solution.
3. Click on 'Configure Application Insights'

![Configure Application Insights](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7uiltt0o127j4ssz3vqi.png)

4. This will open a pop up asking for connecting to the dependency. If you need to use application insights on local machine, click on Application Insights SDK (Local). If you want to connect to an instance on Azure, click on Azure Application Insights. For this post, we will be connecting to an instance on Azure.

![Connect to dependency](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vushcbs1igjczt70vt5i.png)

5. On clicking next, we need to select the Application Insights instance. If not present, there is an option to create a new and then select it.

![Select the Application Insights instance on Azure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4r37ush91zqzak9nwbv.png)

6. Next, it will show us the Application Insights connection string which is to be saved in the User secret file.

7. On the next step, it will add the necessary config files and Nuget packages to the application.

![Files added to the project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izhnuc6sa0c1rqj3d5kk.png)




## Deployment and Application Insights portal

Once the changes are deployed, you can visit the Azure portal to check the newly created Application Insights instance. It will start showing stats for your application in some time. 


![Application Insights Portal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nldap8dglzir1h3admcg.png)


## Checking Failures in API

Here's an example on how  we can check the exceptions which were throw by the API and their details.
In the overview page of Application Insights, we will see a **Failures** chart. This option is also present in the left pane.

![Failures](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/msp49wt3ihc9rilggh59.png)

Once you click on any of the above highlighted option, you'll get the list of the failures along with the exceptions by HTTP Status code, exception types etc. along with the operation name.

![Failure details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cd8xqcr7gpgfzcutcz27.png)

If we click on the type of exception, we will get complete list of the exception of the selected type. We can then click on any item from the list to get the complete details about the exception.



![List of exceptions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8s94iw5s5yg7fct5xcg.png)





![Exception Details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vf6ik4n4aa83b4r3nfgw.png)