자습서: Kusto C# SDK를 Insights에 연결

이 가이드는 Insights와 함께 Kusto C# SDK 사용을 시작하는 데 도움이 됩니다. 연결한 후 Azure 함수에서 Insights를 쿼리할 수 있습니다. Insights를 연결할 수 있는 다른 도구에 대한 자세한 내용은 Insights에 외부 도구 연결을 참조하세요.

필수 조건

AAD로 인증된 PlayFab 계정

Microsoft가 인증 공급자로 설정된 PlayFab 계정 또는 사용자가 필요합니다. Microsoft 인증 공급자는 Azure 서비스를 이용하는 데 필요한 인증에 AAD(Azure Active Directory)를 사용합니다. AAD 인증 계정 또는 사용자 만들기에 대한 지침은 게임 관리자에 대한 Azure Active Directory 인증에서 참고하세요.

계정 또는 사용자가 Microsoft 인증 공급자를 사용하도록 설정되어 있는지 확인하려면 다음과 같이 하세요.

  • developer.playfab.com을 방문합니다.
  • Microsoft에 로그인을 선택하여 PlayFab 계정에 액세스합니다.

로그인할 수 있으면 계정이 Microsoft 인증 공급자를 사용하도록 설정됩니다.

Insights에 대한 게임 관리자 권한

다음 게임 관리자 권한을 사용하도록 설정된 계정에 사용자 역할을 할당해야 합니다.

  • 관리자 상태입니다.
  • 탐색기 탭 및 관련 데이터에 액세스합니다.
  • 분석 데이터에 대한 읽기 및 쓰기 액세스 권한입니다.

새 사용자 역할을 만들거나 기존 역할에 이러한 권한을 추가할 수 있습니다.

기타 필수 구성 요소

패키지 설치

  1. 다음 NuGet 패키지를 설치합니다.

  2. 다음은 시작하기 위한 몇 가지 샘플 코드입니다.

namespace HelloPlayFabInsights
{
    using System;
    using Kusto.Data;
    using Kusto.Data.Common;
    using Kusto.Data.Net.Client;

    class Program
    {
        const string Cluster = "https://insights.playfab.com";
        const string Database = "<title id>";
        const string AzureAuthority = "microsoft.onmicrosoft.com";
        const string ClientId = "<app id>";
        const string ClientSecret = "<app secret>";

        static void Main()
        {
            var kcsb = new KustoConnectionStringBuilder(Cluster, Database).WithAadApplicationKeyAuthentication(ClientId, ClientSecret, AzureAuthority);

            Console.WriteLine("Run Query...");
            RunQuery(kcsb);

            Console.WriteLine("Run Command...");
            RunCommand(kcsb);
            
        }
    
        /// <summary>
        /// Run a query on your Insights database, and write the results to the console.
        /// </summary>
        /// <param name="kcsb"></param>
        static void RunQuery(KustoConnectionStringBuilder kcsb)
        {
            using (var queryProvider = KustoClientFactory.CreateCslQueryProvider(kcsb))
            {
                var query = "['events.all'] | limit 10";

                var clientRequestProperties = new ClientRequestProperties() {
                    Application = "DotNetSDK",
                    ClientRequestId = Guid.NewGuid().ToString() 
                };
                using (var reader = queryProvider.ExecuteQuery(query, clientRequestProperties))
                { 
                    while (reader.Read())
                    {
                        string name = reader.GetString(2);
                        string titleId = reader.GetString(5);
                        DateTime timestamp = reader.GetDateTime(8);
                        Console.WriteLine("{0}\t{1}\t{2}", name, titleId, timestamp);
                    }
                }
            }
        }

        /// <summary>
        /// Run the ".show tables" command on your Insights database, and write the results to the console.
        /// </summary>
        /// <param name="kcsb"></param>
        static void RunCommand(KustoConnectionStringBuilder kcsb)
        {
            using (var commandProvider = KustoClientFactory.CreateCslAdminProvider(kcsb))
            {
                var command = ".show tables";

                var clientRequestProperties = new ClientRequestProperties()
                {
                    Application = "DotNetSDK",
                    ClientRequestId = Guid.NewGuid().ToString()
                };
                using (var reader = commandProvider.ExecuteControlCommand(command, clientRequestProperties))
                {
                    while (reader.Read())
                    {
                        string tableName = reader.GetString(0);
                        string databaseName = reader.GetString(1);
                        Console.WriteLine("{0}\t{1}", tableName, databaseName);
                    }
                }
            }
        }
    }
}

추가 리소스