Azure Application Insights is a cloud based application monitoring service from Microsoft. You can monitor your applications in all of your environments with Application Insights, including development and production. Microsoft has been shipping Visual Studio with built-in support for Application Insights in Windows. However, Visual Studio for Mac does not yet have the ability to add Application Insights to projects. In this post, I will show you how to add Application Insights telemetry to a .NET Core API using Visual Studio for Mac.

Steps to setup Azure Application Insights telemetry for .NET Core API in Visual Studio for Mac:

  • Create the resource on Azure
  • Manually add package reference to the API project
  • Configure API project with the Instrumentation Key

Getting started with Application Insights

First of all, create your free Azure account here

Head over to the portal to create a new resource. Search for ‘application insights’ and click Create.

creating app insights

It will take some time (usually a few seconds) to create the new Application Insights resource. Then, navigate to the resource and find your Instrumentation Key from the right hand side of the screen.

The instrumentation key uniquely identifies the resource you created on Azure. You can use this key with as many components as you wish, including APIs and front end apps.

Configure project

If your project is open in Visual Studio, close it and open the csproj file in TextEdit or a text editor.

Add the following package reference to an ItemGroup.

<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.2.0"/>
Code language: HTML, XML (xml)

Open the project in Visual Studio for Mac and build. This will install all necessary packages for the project to send telemetry data to Azure.

Next, open the configuration file of your project – usually config.json – and add the Instrumentation Key.

... }, "ApplicationInsights": { "InstrumentationKey": "xxxx-yyyy-zzzz" } }
Code language: JSON / JSON with Comments (json)

If you happen to have multiple environments like staging, you need to create a separate Application Insights resource for each environment. Then, add the instrumentation key in each corresponding configuration file.

Finally, complete the configuration of Application Insights in ConfigureServices inside of Startup.cs:

services.AddApplicationInsightsTelemetry(Configuration);
Code language: C# (cs)

Application Insights framework reads the whole configuration and interprets your instrumentation key.

3. Monitor Your Application

That’s it! Let’s see some live telemetry.

Startup the project and make a few calls to the API. Head over to the resource in Azure and you’ll see the results live!

Summary

We have seen how easy it is to setup Azure Application Insights for .NET Core Web APIs.

If you have an Angular app, you can enable end-to-end monitoring read my post on how to monitor Angular using Application Insights.

Umut Esen

I am a software developer and blogger with a passion for the world wide web.

Leave a Reply

This Post Has One Comment

  1. Khaled Saleh

    Loved it, thanks for sharing!