'Application Insights from last Debug session

I'm encapsulating TelemetryClient functionality to a framework component for both client and server to use. In the process, trimming dependencies and replacing default behavior with my own.

My problem, however, is although telemetry shows up in debug output, it's not shown in the Application Insights Search window.

AISearch

Note, the telemetry is picked up in Azure Portal.

How can I get data from debug session telemetry, without the full blown Web App to Add Application Insights Telemetry... workflow?

Steps to reproduce:

  • Create Azure resource, replace InstrumentationKey = "###YourKey###" in snippet below
  • Create .NET Framework ConsoleApp
  • Add Microsoft.ApplicationInsights nuget package

Program.cs

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Track.AddEvent("Debugging");
            System.Console.WriteLine("Search Insights");
            System.Console.ReadLine();
        }
    }

    public static class Track
    {
        private static readonly TelemetryClient TelemetryClient;

        static Track()
        {
            TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();
            config.InstrumentationKey = "###YourKey###";
            TelemetryClient = new TelemetryClient(config);
        }

        public static void AddEvent(string eventName)
        {
            TelemetryClient.TrackEvent(eventName);
        }
    }
}

Taking data from Azure Resource.

AzRes



Solution 1:[1]

Don't know what caused it, but you can get around by manually adding ApplicationInsights.config.

Workflow:

  • Right click project: Add new item...
  • Choose: Application Configuration File
  • Name: ApplicationInsights.config

Running the solution again made the event count appear next to the light bulb.

  • Go to Application Insights Search window
  • Check All
  • Update

Data from Debug session telemetry shown accordingly.

Funny thing, if you now remove the config file, the events will still show up in the Application Insights Search window, although the count next to the light bulb disappears again.

Solution 2:[2]

I just copy your code and install the latest version of Microsoft.ApplicationInsights(2.9.1), works fine at my side: I can see the telemetry data is shown in the "Application insights search".

  1. So can you confirm the version of the visual studio? Seams that there is issue in some previously visual studio version . I'm using 15.8.5, and works fine.

enter image description here

  1. Can you confirm the time range you select is correct?

  2. Please check if you can see the count of telemetry data in your visual studio as per screenshot below:

enter image description here

  1. Also, if there is an update button, please click it to get latest data.

enter image description here

Solution 3:[3]

Be aware that by default, Application Insights nuget uses appsettings.json independently of your IConfiguration. Which is silly IMHO.

If you don't use appsettings.json files, your local AI will not capture your telemetry information. You can add empty appsettings.json and appsettings.Development.json into your project folder...

...or you can remove the configuration registration and/or add your own. Local AI telemetry will start working after that.

services.AddApplicationInsightsTelemetry();

// remove default AI config that reads appsettings.json
var invalidAiDescriptor = services.FirstOrDefault(x => x.ServiceType.Equals(typeof(IConfigureOptions<ApplicationInsightsServiceOptions>)));
services.Remove(invalidAiDescriptor);

// re-configure with your own IConfiguration object if you want
services.Configure<ApplicationInsightsServiceOptions>(configuration);

Solution 4:[4]

I had been unable to solve this problem for some time. This problem is still present with Visual Studio 2022, but the accepted answer ("Add an ApplicationInsights.config file") doesn't work (in VS 2019 or VS 2022).

I had tried a number of other approaches including Visual Studio tooling for adding a "Connected Service Dependency" on Application Insights SDK (Local), which seems like it should work.

Connected Service Dependency Screenshot

It adds Properties/serviceDependencies.json and Properties/serviceDependencies.local.json, which look like they tell Visual Studio that the project uses App Insights, but still no data when debugging in the App Insights window.

After searching around on this problem again, I found a fix here. Add the following to the .csproj file being debugged:

  <PropertyGroup>
    <!-- Needed for Visual Studio to "see" local App Insights data in debug output. -->
    <ApplicationInsightsResourceId>dummy-value</ApplicationInsightsResourceId> 
  </PropertyGroup>

This works even with no dummy ApplicationInsights.config file, and without the serviceDependencies.json file; and the presence of those files does not work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Funk
Solution 2
Solution 3 Mirek
Solution 4 crimbo