azure-application-insightsazure-log-analyticsazure-media-servicesazure-monitoringazure-media-player

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


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 = "";
        });

I have tried the class TelemetryClien, but i havent found any methods to get this data in a c# class. LogsQueryCLient whith the method "QueryWorkspace" is a class that i have found interesting but im not sure if it is related with application Insights.


Solution

  • TelemetryClient is a class used to Send telemetry. You cannot read telemetry using the Application Insights SDK

    To query data you can indeed use the LogsQueryCLient class from the Azure.Monitor.Query NuGet package.

    Azure Application Insights and Azure Log Analytics, including the workspaces are part of Azure Monitor. There are two flavours for Application Insights: classic and Workspace-based. Workspace-based Application Insights resources store the data inside Log Analytic workspaces, hence you need a workspace to be able to use the LogsQueryCLient class.

    Here is some example code

    string workspaceId = "<workspace_id>";
    var client = new LogsQueryClient(new DefaultAzureCredential());
    Response<LogsQueryResult> response = await client.QueryWorkspaceAsync(
        workspaceId,
        "AzureActivity | top 10 by TimeGenerated",
        new QueryTimeRange(TimeSpan.FromDays(1)));
    
    LogsTable table = response.Value.Table;
    
    foreach (var row in table.Rows)
    {
        Console.WriteLine(row["OperationName"] + " " + row["ResourceGroup"]);
    }
    

    (source)