win-universal-appwindows-10-universalazure-application-insights

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


I have a simple Windows Universal Application with Azure Application Insights enabled in it. First published version worked well - all telemetry data was visible in AppInsights dashboard. But after I updated the app, strange thing happened - no telemetry data is generated at all.

After installing the version of the application from Windows Store, and tracing web requests with Fiddler, I've found out VERY strange thing - instrumentation key (iKey) sent in AppInsights requests is UNKNOWN for me. I mean that this is not a one of my AppInsights instances!

On the other hand, when I sideload the same package I've used for publish, than correct (expected) iKey is used in traced web requests.

This makes me think that my iKey is somehow replaced in published version of application. How is this possible?

IMPORTANT NOTE: AppInsights initialization code is the following (in 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);

    // ...
}

And one more time - this worked well in first version of the application and was broken only after update (which did not touch any AppInsights related stuff).

UPDATE (2016-03-21): Following recommendations in the answers, I've done the following:

  1. Moved release iKey into ApplicationInsights.config file

  2. Changed initialization logic to replace the iKey only in case of Debug mode:

     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. Republished the application to Windows Store.

Now AppInsights is working correctly - my iKey is used in AppInsights web requests and data appears in admin panel.


Solution

  • do you have an applicationinsights.config file in your project?

    if you do not, I believe the store will create one for you so that the reports work, because it doesn't know you're setting the key in code. you might want to add an applicationinsights.config file, and put in one of your ikeys, even if you are then setting the key explicitly in code.