在 localhost 中使用 secrets.json 时,事件未流入 Application Insights

Sco*_*Lin 2 azure-application-insights asp.net-core-2.0

我使用此Application Insights 入门指南来设置我的应用程序。我正在运行VS2017 version 15.5.7,我的应用程序正在使用AspNetCore 2.0.0.

当我从 Visual Studio 使用 IIS Express F5 调试我的应用程序时,我在调试器事件窗口中看到 Application Insights 事件。但是,相同的事件不会流向 Azure 中的 Application Insights;我已经配置了InstrumentationKey内部secrets.json,如下所示。我已经通过设置断点确认密钥已加载到我的应用程序配置中。

作为另一个调试数据点,我已经确认在 Azure Web 应用程序中运行时,事件确实成功流向 Application Insights。此测试使用完全相同的代码。InstrumentationKey但是,我没有从 secrets.json加载,而是将APPINSIGHTS_INSTRUMENTATIONKEY环境变量配置为具有密钥(通过ApplicationSettings门户中的 Web 应用程序窗格)。

我不知道为什么我的事件没有通过本地主机调试器流到 Application Insights,但它们在部署到 Web 应用程序时。

程序.cs

public static void Main( string[] args )
{
    BuildWebHost( args ).Run();
}

public static IWebHost BuildWebHost( string[] args ) =>
    WebHost.CreateDefaultBuilder( args )
        .UseStartup<Startup>()
        .UseApplicationInsights()
        .Build();
Run Code Online (Sandbox Code Playgroud)

启动文件

var configurationBuilder = new ConfigurationBuilder()
    .SetBasePath( hostingEnvironment.ContentRootPath )
    .AddJsonFile( $"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true );

if ( hostingEnvironment.IsDevelopment() )
{
    configurationBuilder.AddUserSecrets<Startup>( optional: true );
}

this.Configuration = configurationBuilder
    .AddEnvironmentVariables()
    .Build();
Run Code Online (Sandbox Code Playgroud)

秘密.json

{
    "ApplicationInsights": {
        "InstrumentationKey": "omitted for StackOverflow"
    }
}
Run Code Online (Sandbox Code Playgroud)

我的.csproj

<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.1.1" />
<PackageReference Include="Microsoft.ApplicationInsights.Web" Version="2.5.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
Run Code Online (Sandbox Code Playgroud)

启动设置.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
        "applicationUrl": "http://localhost:2322/",
        "sslPort": 44330
    }
  },
  "profiles": {
    "IIS Express": {
        "commandName": "IISExpress",
        "launchBrowser": true,
        "launchUrl": "https://localhost:44330/",
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        }
    },
    "FrontDoor": {
        "commandName": "Project",
        "launchBrowser": true,
        "launchUrl": "https://localhost:44330/api/config",
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        },
        "applicationUrl": "http://localhost:2323"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*ner 5

  1. 当您在调试器输出窗口中看到 appinsights 事件时,这些事件是否(unconfigured)在其中显示?如果它出现在输出窗口中,则意味着 sdk 正在调试中运行,但未在您的appsettings.json(或在代码中)找到检测密钥 请注意,应用程序洞察 sdk 不会在用户机密中查找此设置,仅应用程序设置!2020 年 5 月 28 日编辑:请参阅下面的 OP 编辑​​,这是 2 年前的问题,如果您使用AddApplicationInsightsTelemetry而不是过时的,则UseApplicationInsights此限制不适用)

  2. 如果在调试器窗口中的事件(unconfigured),下一步是使用类似小提琴手看你出站的HTTP流量,以确保您所看到的出站通过SDK调用去dc.services.visualstudio.com,和那些出站呼叫是成功的。(在此步骤中,您可以验证您认为正在使用的检测密钥是否是 sdk 用于发送事件的密钥)

  3. 如果事件正在发送,并且正在使用您设置的 ikey,那么最后一步是验证您使用的 ikey 是您在门户中查看的资源的 ikey。每隔一段时间,就会有人在某处拥有“dev”的 ikey,然后在门户中查看“prod”资源却没有看到他们期望的事件。

  4. 如果您已经到了这一步,事件被发送,发送到您想要的 ikey,并验证 ikey 适用于您期望的资源,然后验证没有任何服务中断或延迟可能是影响您的数据,您可以在http://aka.ms/astatus 上找到, 否则,您通常应该在几秒到几分钟内看到事件,具体取决于您在世界上的哪个位置以及您的资源在天蓝色的区域等。

OP 编辑​​以确保答案的完整性:

事实上,我打到了#1,正如约翰在评论中暗示的那样,我需要以另一种方式让 AI SDK 了解我的 iKey。我没有使用UseApplicationInsights()in Program.cs,而是切换到使用AddApplicationInsightsTelemetry(configuration)inside StartUp.ConfigureServices()。传递的配置使我的 iKey 从secrets.json.