Search code examples
c#azure-application-insights

Adding "Add Standard test" equivalent "Availability" event in Azure Application Insights from C# Console Application


I am writing a C# console application to add "Add Standard test" equivalent "Availability" event in Azure Application Insights. Here is code:

  
        TelemetryConfiguration configuration = new TelemetryConfiguration();

        configuration.ConnectionString = "InstrumentationKey=5xxx-xxx-xxx;IngestionEndpoint=https://usxxxxxx.in.applicationinsights.azure.com/;LiveEndpoint=https://usxxxxxx.livediagnostics.monitor.azure.com/";
        
        var client = new TelemetryClient(configuration);
        var counter = 0;
        while (counter < 10)
        {
            try
            {
                var stopwatch = Stopwatch.StartNew();
                Thread.Sleep(5000); 
                stopwatch.Stop();

                var success = true; // Set test Result success
                var durationMs = stopwatch.ElapsedMilliseconds;

                // Create AvailabilityTelemetry
                var availabilityTelemetry = new AvailabilityTelemetry
                {
                    Name = "AvailabilityTest",
                    Success = success,
                    Duration = TimeSpan.FromMilliseconds(durationMs),
                    Timestamp = DateTimeOffset.UtcNow,
                    Message = "System is available",
                };

                // Send availability telemetry
                client.TrackAvailability(availabilityTelemetry);

                client.Flush();

                Console.WriteLine("Availability event sent successfully!");
            }
            catch (Exception ex)
            {
                client.TrackException(ex);
                // Log error details
                Console.WriteLine($"Error: {ex.Message}");
            }

            counter++;
        }

This code is generating metadata in the activity log. enter image description here

But it is not creating any availability events. When I click on the "Availability" menu under "Investigate" on the left panel of the Application insight, it can't find any availability data to show.

What I am missing here?

I tried to google any example, or tutorial, no luck.

Thanks in advance. Ruhul

I tried to google any example, or tutorial, no luck.


Solution

  • Below is the code (modified your code a little bit) which worked for me:

    using System.Diagnostics;
    using Microsoft.ApplicationInsights;
    using Microsoft.ApplicationInsights.DataContracts;
    using Microsoft.ApplicationInsights.Extensibility;
    
    class Program
    {
        static void Main(string[] args)
        {
            TelemetryConfiguration rith_config = new TelemetryConfiguration();
            rith_config.InstrumentationKey = "5681d8dc-fe6a2";
    
            var rith_client = new TelemetryClient(rith_config);
            var rith_count = 0;
    
            while (rith_count < 10)
            {
                try
                {
                    var rith_stopwatch = Stopwatch.StartNew();
                    Thread.Sleep(5000);
                    rith_stopwatch.Stop();
    
                    var rith_success = true; 
                    var durationMs = rith_stopwatch.ElapsedMilliseconds;
                    var rith_availabilityTelemetry = new AvailabilityTelemetry
                    {
                        Name = "AvailabilityTest",
                        Success = rith_success,
                        Duration = TimeSpan.FromMilliseconds(durationMs),
                        Timestamp = DateTimeOffset.UtcNow,
                        Message = "Testing availability logs",
                    };
                    rith_client.TrackAvailability(rith_availabilityTelemetry);
                    rith_client.Flush();
                    Console.WriteLine("Hello Rithwik Bojja, Availability event sent successfully to Application Insights!");
                }
                catch (Exception ex)
                {
                    rith_client.TrackException(ex);
                    Console.WriteLine($"Hello Rithwik Bojja, Error is : {ex.Message}");
                }
    
                rith_count++;
            }
            rith_client.Flush();
        }
    }
    

    You need to give just instrumentation key not the full connection string in my code and changed the flush and some other parts of code.

    Output:

    enter image description here

    enter image description here

    enter image description here