Asp.Net Core and Application Insight in VS2017 - multiple environments

烂漫一生 提交于 2019-12-12 04:00:06

问题


Prior VS2017, it was possible to setup Application Insight integration into an Asp.NET Core application in the code. In VS2017, it is only possible using the IDE(Connected Services) as the "Microsoft.ApplicationInsights.AspNetCore"(2.0.0.) does not offer builder.AddApplicationInsightsSettings(developerMode: true); extension anymore. All the related resources does not work for VS2017(ie https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Getting-Started).

When using new VS2017 feature "Connected Services", how we are supposed to connect to different Application Insights instances per environment?


回答1:


Ok, it is still possible to set ApplicationInsights manually using ApplicationInsightsServiceOptions. Here is the source code, how the settings are actually resolved:

internal static void AddTelemetryConfiguration(IConfiguration config, ApplicationInsightsServiceOptions serviceOptions)
{
  string str1 = config["APPINSIGHTS_INSTRUMENTATIONKEY"];
  if (string.IsNullOrWhiteSpace(str1))
    str1 = config["ApplicationInsights:InstrumentationKey"];
  if (!string.IsNullOrWhiteSpace(str1))
    serviceOptions.InstrumentationKey = str1;
  string str2 = config["APPINSIGHTS_DEVELOPER_MODE"];
  if (string.IsNullOrWhiteSpace(str2))
    str2 = config["ApplicationInsights:TelemetryChannel:DeveloperMode"];
  if (!string.IsNullOrWhiteSpace(str2))
  {
    bool result = false;
    if (bool.TryParse(str2, out result))
      serviceOptions.DeveloperMode = new bool?(result);
  }
  string str3 = config["APPINSIGHTS_ENDPOINTADDRESS"];
  if (string.IsNullOrWhiteSpace(str3))
    str3 = config["ApplicationInsights:TelemetryChannel:EndpointAddress"];
  if (!string.IsNullOrWhiteSpace(str3))
    serviceOptions.EndpointAddress = str3;
  string str4 = config["version"];
  if (string.IsNullOrWhiteSpace(str4))
    return;
  serviceOptions.ApplicationVersion = str4;
}

So you can see the highest priority have the Environment Variables. You can set the APPINSIGHTS_INSTRUMENTATIONKEY variable in Azure Application settings and it will be picked up.

If the VS2017 Connected Services setup is used, it stores its configuration into csproj, appsettings.json(InstrumentationKey) and /Connected Services/Application Insights/ConnectedServices.json.



来源:https://stackoverflow.com/questions/43943182/asp-net-core-and-application-insight-in-vs2017-multiple-environments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!