'How to configure ApplicationInsight to get connection string from web.config or keyvault or programmatically set it?
I am trying to setup dev, test and production environment in Azure for my ASP.NET MVC application. Each environment has their own application insight service. So, I want to get app insight connection string value from outside source for each environment to publish correct value. The source could be web.config file or keyvault. Other suggestions also welcomed.
I tried programmatically to create telemetryclient by supplying config value, but it didn't work. The client ignores the connection string I manually added and always gets the connectionstring value from ApplicationInsight.config file.
Any suggestion?
Solution 1:[1]
Adding following instrumentation key and connection string to Global.asax.cs file and solved my problem. Each environment will get their values from keyvault.
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.AppSettings["APPINSIGHT_INSTRUMENTATION_KEY"];
TelemetryConfiguration.Active.ConnectionString = ConfigurationManager.AppSettings["APPINSIGHT_INSTRUMENTATION_CENNECTIONSTRINGS"];
}
Solution 2:[2]
Since your logs should not be reached from other resources it's better to use the key vault. After configuring key vault in program you can retrieve application insight key from keyvault.
.ConfigureLogging((context, builder) => { var insightsSecret = context.Configuration.GetValue<string>(InstrumentationKey); builder.AddApplicationInsights(insightsSecret); });
The basic setup for app insight should look like this for Program.cs
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Maria21 |
| Solution 2 |
