This post revisits a previous post “Don’t forget to flush” Application insights and shows how to configure the instrumentation key in code or via the ApplicationInsights.config file.
class Program
{
static void Main(string[] args)
{
#if INSTRUMENTATION_KEY_TELEMETRY_CONFIGURATION
if (args.Length != 1)
{
Console.WriteLine("Usage AzureApplicationInsightsClientConsole <instrumentationKey>");
return;
}
TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration(args[0]);
TelemetryClient telemetryClient = new TelemetryClient(telemetryConfiguration);
telemetryClient.TrackTrace("INSTRUMENTATION_KEY_TELEMETRY_CONFIGURATION", SeverityLevel.Information);
#endif
#if INSTRUMENTATION_KEY_APPLICATION_INSIGHTS_CONFIG
TelemetryClient telemetryClient = new TelemetryClient();
telemetryClient.TrackTrace("INSTRUMENTATION_KEY_APPLICATION_INSIGHTS_CONFIG", SeverityLevel.Information);
#endif
telemetryClient.TrackTrace("This is an AI API Verbose message", SeverityLevel.Verbose);
telemetryClient.TrackTrace("This is an AI API Information message", SeverityLevel.Information);
telemetryClient.TrackTrace("This is an AI API Warning message", SeverityLevel.Warning);
telemetryClient.TrackTrace("This is an AI API Error message", SeverityLevel.Error);
telemetryClient.TrackTrace("This is an AI API Critical message", SeverityLevel.Critical);
telemetryClient.Flush();
Console.WriteLine("Press <enter> to exit");
Console.ReadLine();
}
A sample project is available here
Pingback: “Don’t forget to flush” .Net Core Application Insights | devMobile's blog