How to get the data from Application Insights in a C# class (Azure Media Player Plugin)

Mario Lada Martinez 20 Reputation points
2023-03-06T09:06:43.1566667+00:00

Hello, im currently trying to get event data from application insights from a c# class. Im going to save information about the assets reproduced in azure media player, like visits count, time watched and other stuff like that.

My tracking data and my code is the following, and the event name where i save this info is "AssetPlayBackData". This event is tracked everytime a video is played on the azure media player. Now, how can i get the all the events called "AssetPlayBackData" from a c# class? I have read i should use LogsQueryClient with the method "QueryWorkspace" but im not sure. Thanks in advance!

  var trackingData = {
            assetName: "",
            playStartTime: null,
            playEndTime: null,
            visitCount: 0,
            playbackDuration: 0,
            location: ""
        };

        player.on('play', function () {
            var currentSrc = player.currentSrc();
            var assetName = currentSrc.substring(currentSrc.lastIndexOf("/") + 1);
            trackingData.assetName = assetName;
            trackingData.visitCount++;
            trackingData.playStartTime = new Date().toISOString();
            trackingData.location = window.navigator.language;
        });

        player.on('ended', function () {
            trackingData.playEndTime = new Date().toISOString();
            trackingData.playbackDuration = (new Date(trackingData.playEndTime) - new Date(trackingData.playStartTime)) / 1000;      
            appInsights.trackEvent("AssetPlaybackData", trackingData);

            trackingData.playStartTime = null;
            trackingData.playEndTime = null;
            trackingData.visitCount = 0;
            trackingData.playbackDuration = 0;
            trackingData.location = "";
        });
Azure Media Services
Azure Media Services
A group of Azure services that includes encoding, format conversion, on-demand streaming, content protection, and live streaming services.
305 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Musaif Shaikh 85 Reputation points
    2023-03-06T10:04:23.2933333+00:00

    o retrieve all events with the name "AssetPlaybackData" from Application Insights using C#, you can indeed use the LogsQueryClient class provided by the Azure.Monitory.Query package.

    Here's some sample code that shows how to use the LogsQueryClient to retrieve all events with the name "AssetPlaybackData" from a given Application Insights resource:

    using Azure.Identity;
    using Azure.Monitor.Query;
    using Azure.Monitor.Query.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    public class AppInsightsQuery
    {
        public async Task<List<LogsQueryResult>> GetAssetPlaybackDataEventsAsync(string workspaceId, string appId, string clientSecret)
        {
            var tenantId = "<your-tenant-id>";
            var credential = new ClientSecretCredential(tenantId, appId, clientSecret);
            var client = new LogsQueryClient(credential);
    
            // The query to retrieve events with the name "AssetPlaybackData"
            var query = $"AppEvents | where name == 'AssetPlaybackData'";
    
            // The workspace ID for your Application Insights resource
            var workspace = $"/subscriptions/<your-subscription-id>/resourcegroups/<your-resource-group>/providers/microsoft.operationalinsights/workspaces/{workspaceId}";
    
            var result = await client.QueryAsync(workspace, query);
            return result.ToList();
        }
    }
    
    

    To use this code, you'll need to provide your Azure AD tenant ID, your Application ID, your client secret, your subscription ID, and your resource group name. You'll also need to specify the workspace ID for your Application Insights resource.

    Once you have this code, you can call the GetAssetPlaybackDataEventsAsync method to retrieve all events with the name "AssetPlaybackData" from your Application Insights resource. The method returns a list of LogsQueryResult objects, which contain the event data.