当 Windows 通用应用程序发布到 Windows 商店时,App Insights 检测密钥被替换

AppInsights instrumentation key is replaced when win universal app is published to Windows Store

我有一个简单的 Windows 通用应用程序,其中启用了 Azure Application Insights。首次发布的版本运行良好——所有遥测数据都在 AppInsights 仪表板中可见。但是在我更新应用程序后,奇怪的事情发生了——根本没有生成遥测数据。

从 Windows Store 安装应用程序版本并使用 Fiddler 跟踪 Web 请求后,我发现了一件非常奇怪的事情 - AppInsights 请求中发送的检测密钥 (iKey) 对我来说是未知的。我的意思是这不是我的 AppInsights 实例之一!

另一方面,当我旁加载用于发布的同一个包时,跟踪的 Web 请求中使用了正确的(预期的)iKey。

这让我觉得我的 iKey 在已发布的应用程序版本中以某种方式被替换了。 这怎么可能?

重要提示: AppInsights 初始化代码如下(在 App class 中):

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    var instrumentationKey =
#if DEBUG
    Current.Resources["ApplicationInsights.InstrumentationKey.Debug"].ToString();
#else
    Current.Resources["ApplicationInsights.InstrumentationKey.Release"].ToString();
#endif
    Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(instrumentationKey);

    // ...
}

还有一次 - 这在应用程序的第一个版本中运行良好,仅在更新后才被破坏(没有触及任何与 AppInsights 相关的东西)。

更新 (2016-03-21): 根据答案中的建议,我做了以下工作:

  1. 将发布的 iKey 移动到 ApplicationInsights.config 文件
  2. 更改了初始化逻辑以仅在调试模式下替换 iKey:

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
    #if DEBUG
        var instrumentationKey = Current.Resources["ApplicationInsights.InstrumentationKey.Debug"].ToString();
        await Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(instrumentationKey);
    #else
        await Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync()
    #endif
    
        // ...
    }
    
  3. 已将应用程序重新发布到 Windows 商店。

现在 AppInsights 工作正常 - 我的 iKey 用于 AppInsights 网络请求并且数据出现在管理面板中。

您的项目中有 applicationinsights.config 文件吗?

如果您不这样做,我 相信 商店会为您创建一个以便报告有效,因为它不知道您正在代码中设置密钥。您可能想要添加一个 applicationinsights.config 文件,并放入您的 ikeys 之一,即使您随后在代码中显式设置密钥也是如此。

适用于商店应用的 ApplicationInsights SDK,仅适用于 ApplicationInsights.config。

商店查找此文件,如果未设置检测密钥,则会注入一个。

另一方面,SDK 将始终遵守来自配置文件的检测密钥。