How to add Azure Application Insights to a .NET Core App?

Viewed 4201

I'm trying to add Azure Application Insights to my .NET Core App so I can see search traffic analytics but I only keep finding documentation on how to set this up for an ASP.NET App, is setting Azure Application Insights possible for a .NET Core App?

Thanks for your time,

3 Answers

An overall solution that would work locally, on both Windows and on Linux* environments, is:

  1. Create an Application Insights account.

  2. Install the Microsoft.ApplicationInsights.AspNetCore NuGet package

  3. Using Dependency Injection, register the Application Insights client into the service collection. You can find a very good example on how to do this on Andrew Lock's blog.

  4. Register Application Insights telemetry insight your Startup class. It will work out of the box for you later on.

    services.AddApplicationInsightsTelemetry();
    
  5. Add ApplicationInsights in your appsettings file, and InstrumentationKey inside of it (Your instrumentation key can be found in the overview tab of your resource):

    "ApplicationInsights": {
      "InstrumentationKey": "…"
    },
    

Now, if you run your application, you will be able to capture all traffic in your Application Insights account.

* On Linux, this is currently in preview on Azure, but this would work.

Open your project in Visual Studio, right click in project you want to add the Application insights and click Publish. Go to Connected Services, inside Service Dependencies, click Add Dependency and select Application Insights Sdk (Local) as shown in the screenshot below.

enter image description here

Click Next then click Finish to install the required Nuget packages and you're done.

Related