3

No matter what I try, Application Insights in a simple C# Console App (NetCORE) will not pickup the InstrumentationKey. I am using the following code:

var telemetryClient = new TelemetryClient();
Console.WriteLine("Key=" + telemetryClient.InstrumentationKey);

And the following ApplicationInsights.config file:

<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
  <InstrumentationKey>1234</InstrumentationKey>
  <!--https://learn.microsoft.com/en-us/azure/azure-monitor/app/configuration-with-applicationinsights-config#instrumentationkey-->
</ApplicationInsights>

The key is always empty.

I have tried wrapping the config contents in and I have tried removing the tag. I have tried the following code too:

var telemetryClient = new TelemetryClient(TelemetryConfiguration.CreateDefault());
Console.WriteLine("Key=" + telemetryClient.InstrumentationKey);

My previous question on the subject resulted in a solution that required the ApplicationInsights.config file to exist in the project folder for the debug application insights logger to work correctly. Link: Unable to get Application Insights to show debugging telemetry . One of the users notes that NetCORE is no longer using that config file, yet I find that hard to believe as otherwise no telemetry is logged in the debug Application Insights Telemetry.

The documentation for it does not mention InstrumentationKey inside of the config file: https://learn.microsoft.com/en-us/azure/azure-monitor/app/configuration-with-applicationinsights-config#instrumentationkey

Is there a way to just run those two lines of code and have it pick up the instrumentation key from the config file or do I have to set it up manually in code?

Eduard G
  • 443
  • 5
  • 21

1 Answers1

2

For .NET Core or ASP.NET Core projects SDK does not read applicationinsights.config file. For console apps, the recommended approach is documented here (https://learn.microsoft.com/en-us/azure/azure-monitor/app/worker-service#net-corenet-framework-console-application). Instrumentation key is specified in appsettings.json in those cases.

cijothomas
  • 2,818
  • 1
  • 13
  • 25
  • I see. I find it weird then that it requires ApplicationInsights.config to track Telemetry using the VS Application Insights logging tool for debug sessions, as seen in https://stackoverflow.com/questions/58307327/unable-to-get-application-insights-to-show-debugging-telemetry/58311281#58311281 . – Eduard G Oct 11 '19 at 10:05