App Insights Helper Class
  • 17 Sep 2025
  • 1 Minute to read
  • Contributors
  • Dark
    Light
  • PDF

App Insights Helper Class

  • Dark
    Light
  • PDF

Article summary

Below is the helper class I use for querying the log analytics workspace to get the id for the relevant workflow runs.

using Azure.Identity;
using Azure.Monitor.Query;
using Azure.Monitor.Query.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Playwright.Tests
{
    public class AppInsightsHelper
    {
        private ApplicationInsightsSettings _config;
        private LogsQueryClient _appInsightsClient;


        public AppInsightsHelper(ApplicationInsightsSettings config)
        {
            _config = config;
            var credentials = new ClientSecretCredential(config.TenantId, config.ClientId, config.ClientSecret);
            _appInsightsClient = new LogsQueryClient(credentials);
        }

        public async Task<LogsTable> QueryLog(string kqlQuery, TimeSpan timeSpan, int retries = 10)
        {
            var i = 0;
            while (i < retries)
            {
                var result = await _appInsightsClient.QueryWorkspaceAsync(_config.WorkspaceId, kqlQuery, timeSpan);
                i++;

                if (result.Value != null
                    && result.Value.Table != null
                    && result.Value.Table.Rows.Count > 0)
                {
                    return result.Value.Table;
                }

                Thread.Sleep(new TimeSpan(0, 0, 30));
            }
            return null;
        }

    }
}


Was this article helpful?