This post updates a previous post “Don’t forget to flush Application insights Revisited” for .Net Core 3.X and shows the small change required by the deprecation of on of the TelemetryClient constructor overloads.
warning CS0618: ‘TelemetryClient.TelemetryClient()’ is obsolete: ‘We do not recommend using TelemetryConfiguration.Active on .NET Core. See https://github.com/microsoft/ApplicationInsights-dotnet/issues/1152 for more details’
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
TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.CreateDefault();
TelemetryClient telemetryClient = new TelemetryClient(telemetryConfiguration);
telemetryClient.TrackTrace("INSTRUMENTATION_KEY_APPLICATION_INSIGHTS_CONFIG", SeverityLevel.Information);
#endif
telemetryClient.Context.User.Id = Environment.UserName;
telemetryClient.Context.Device.Id = Environment.MachineName;
telemetryClient.Context.Operation.Name = "Test harness";
telemetryClient.TrackTrace("This is a .Net Core AI API Verbose message", SeverityLevel.Verbose);
telemetryClient.TrackTrace("This is a .Net Core AI API Information message", SeverityLevel.Information);
telemetryClient.TrackTrace("This is a .Net Core AI API Warning message", SeverityLevel.Warning);
telemetryClient.TrackTrace("This is a .Net Core AI API Error message", SeverityLevel.Error);
telemetryClient.TrackTrace("This is a .Net Core AI API Critical message", SeverityLevel.Critical);
telemetryClient.Flush();
telemetryConfiguration.Dispose(); // In real-world use a using or similar approach to ensure cleaned up
Console.WriteLine("Press <enter> to exit");
Console.ReadLine();
}
}
A sample project is available here