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?
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.