How to set-up Azure Application Insights in .NET 4.8 to work for multiple projects in one solution, on multiple environments (QA, Staging, Production)

SL 20 Reputation points
2023-11-29T15:53:19.2633333+00:00

I have a solution with 10 projects - each being deployed as a separate AppService in Azure. For all I resources have created respective ApplicationInsights resource.

I also deploy this to several different environments (QA, Staging, Production). Some of them are in separate subscriptions, some just different resource groups.

Deployments are done via yaml pipelines, and variable values from web.config are kept in keyvaults.

Now, trying to set-up Application Insights to be able to track issues for each of them, in each environment.
The closest solution I found was to set-up variable in web.config for each environments and use that variable to point to specific AppInsight resource. However, not sure should I add that var to ApplicationInsights.config, or can I use web.config that we already use for other vars?
Is there a C# .NET example (not Core) that I could look at?

Also, since there are multiple projects inside a solution, how to best handle vars - I obviously can't use the APPLICATIONINSIGHTS_CONNECTION_STRING value as it will have different value for each project and each environment? Should I introduce different set of vars (for instrumentation key, resource id, resource group, subscription, etc) and where should they be initialized? That would mean 10x4 vars for my case? Any better solution?

Thank you

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,658 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,960 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2023-12-03T03:54:45.6866667+00:00

    Hi @SL

    According to the SDK repo, you can set the instrumentation key programmatically as follows,

    // You do not need this if you have instrumentation key in the ApplicationInsights.config
    TelemetryConfiguration.Active.InstrumentationKey = "Your_Resource_Key";
    System.Diagnostics.Trace.TraceWarning("Slow response - database01");
    

    You can use an application setting in place of Your_Resource_Key. Do note though that is recommended to use the connection string of the application insights resource. As far as I know, it's interchangeable, meaning you can set InstrumentationKey to that value and the telemetry should still flow through. Depending on your scenario and workstream, just using the app insights connection string is all that you should need. Therefore, no need for vars containing the resource group, resource id, subscription, etc.

    If it were me, I would use the above code snippet and leverage app service app settings to set the instrumentation connection string for each web app. The app setting can also be used locally, making management fairly simple. Furthermore, I don't think you need multiple app insight resources. What I would personally do is have a resource for each environment, i.e. production, testing, UAT; an appropriate separation.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.