1. Adding Application Insights to my Blazor app

David Thielen 3,121 Reputation points
2024-08-03T16:44:22.1833333+00:00

Hi all;

Sorry - this is a long one. (But hopefully not that complex.)

I have a Blazor Interactive Server app using ASP.NET version 8. And using Entity Frameworks for accessing the DB and the ASP.NET Identity Library for users & claims.

I initially had Application Insights incorporated in my app using the SDK, adding the code by hand, and turning it on in the Azure App Services blade. When I learned this was duplicative, I then turned it off in the Azure App Services and commented the code out.

I did keep the library Microsoft.ApplicationInsights.AspNetCore added via NuGet. And I kept the Application Insights service I had created in Azure. My Azure App Service has a production slot with 2 instances and the dev slot with 1 instance.

I then ran the Visual Studio: Configure Application Insights… | Azure Application Insights. I then selected my Subscription name and Application Insights instance.

This works! Application Insights is now showing data from my App Service. This question is to make sure it is all configured correctly. And to ask a couple of follow-up questions.

Note I changed any guid to GUID in case any are credentials.

BTW - I read a lot of web pages including this one and watched all 3 Pluralsight classes on Monitoring & Application Insights. The below questions are issues that were either not covered, said something different from what happened, or had conflicting advice.

  1. It added to my app.web.csproj the following. What is this:

<ApplicationInsightsResourceId>/subscriptions/GUID/resourceGroups/Louis_Howe/providers/microsoft.insights/components/LouisHowe</ApplicationInsightsResourceId>

  1. It added the following to my secrets.json. What is all of this?

"APPLICATIONINSIGHTS_CONNECTION_STRING": "InstrumentationKey=GUID;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/;ApplicationId=GUID"

  1. I have left over in secrets.json the following. Do I delete this now as it was from my previous effort to add AI?

"ApplicationInsights:ConnectionString": "InstrumentationKey=GUID;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/"

  1. It added the file Properties/ServiceDependencies/local/appInsights1.arm.json – what is this for?
  2. It added an appInsights1 section to the files Properties/serviceDependencies.json and Properties/serviceDependencies.local.json – what are these for?
  3. It also put the AI connection string in appSettings.json under ApplicationInsights:ConnectionString. I should delete this, keep it in secrets.json, and put this in my KeyVault – correct?
  4. Visual Studio added no code to Program.cs (the documentation said it would). So I added the following. Is this correct? And is the EnableSql needed to get the queries in AI? I expect something so fundamental would be enabled by default.

builder.Logging.AddApplicationInsights();

builder.Services.AddApplicationInsightsTelemetry();

builder.Services.ConfigureTelemetryModule<DependencyTrackingTelemetryModule>((module, o) => { module.EnableSqlCommandTextInstrumentation = true; });

thanks - dave

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,275 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,580 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,777 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. David Thielen 3,121 Reputation points
    2024-08-07T21:46:20.44+00:00

    With no answer here I submitted this to Azure support and got answers. For anyone else with the same questions, here's the answers:

    1. The line on your app.web.csproj (ApplicationInsightsResourceId)/subscriptions/GUID/resourceGroups/Louis_Howe/providers/microsoft.insights/components/LouisHowe(/ApplicationInsightsResourceId) is a reference to your Application Insights resource in Azure. This helps Visual Studio and other tools identify and connect to the correct Application Insights instance. You can find this Resource ID by going into your component > Properties. TL:DR; - ignore this.
    2. You can use this or ApplicationInsights:ConnectionString
    3. You can delete the previous connection string to avoid potential conflicts between the app and the Application Insights component.     
    4. This file is an ARM (Azure Resource Manager) template that defines the resources needed for Application Insights. It helps in deploying and managing the Application Insights resources in Azure. TL:DR; - ignore this.
    5. These sections define the dependencies for your application, specifically the Application Insights resource. They help in configuring and managing the dependencies locally and in the cloud.         TL:DR; - ignore this.
    6. Yes the code I added to Program.cs is correct
    7. What kind of information do you want to see in the queries? If you are looking to get a full SQL query, this may help you: Dependency tracking in Application Insights - Azure Monitor | Microsoft Learn
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.