VS2017中的Asp.Net Core和Application Insight-多种环境 [英] Asp.Net Core and Application Insight in VS2017 - multiple environments

查看:125
本文介绍了VS2017中的Asp.Net Core和Application Insight-多种环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS2017之前,可以在代码中将Application Insight集成到Asp.NET Core应用程序中.在VS2017中,仅可能使用IDE(连接的服务),因为"Microsoft.ApplicationInsights.AspNetCore"(2.0.0.)不再提供builder.AddApplicationInsightsSettings(developerMode: true);扩展名.所有相关资源均不适用于VS2017(即 https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/使用入门).

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).

使用新的VS2017功能连接的服务"时,应该如何在每个环境下连接到不同的Application Insights实例?

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

推荐答案

好,仍然可以使用ApplicationInsightsServiceOptions手动设置ApplicationInsights.这是源代码,这些设置的实际解析方式:

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;
}

因此您可以看到具有环境变量的优先级最高.您可以在Azure应用程序设置中设置APPINSIGHTS_INSTRUMENTATIONKEY变量,它将被选中.

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.

如果使用VS2017 Connected Services设置,它将其配置存储到csprojappsettings.json(InstrumentationKey)和/Connected Services/Application Insights/ConnectedServices.json.

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

这篇关于VS2017中的Asp.Net Core和Application Insight-多种环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆