> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kusto C# SDK を Insights に接続する

> Microsoft Entra ID 認証で Kusto C# SDK を使用し、Azure Functions またはカスタム .NET アプリケーションからレガシー PlayFab Insights のゲーム データをクエリします。

# チュートリアル: Kusto C# SDK を Insights に接続する

このガイドは、Kusto C# SDK を Insights とともに使い始めるのに役立ちます。接続後、Azure Functions から Insights をクエリできます。Insights に接続できる他のツールについては、[外部ツールを Insights に接続する](/services/playfab/data-analytics/legacy/connectivity) を参照してください。

<Note>
  PlayFab Insights Management は 2026/3/31 に廃止されました。今後のパフォーマンスとコストを管理するには、[Azure Data Explorer (ADX) Connections](/services/playfab/data-analytics/export-data/data-connection-adx) の使用をお勧めします。タイトルがまだ **Insights** を使用している場合は、実装の詳細についてこの記事を参照し続けてください。詳細については、[PlayFab Digest: 3 月の機能更新](https://developer.microsoft.com/en-us/games/articles/2026/04/playfab-digest-march-feature-updates/) を参照してください。
</Note>

## 前提条件

### AAD で認証された PlayFab アカウント

認証プロバイダーが Microsoft に設定されている PlayFab アカウントまたはユーザーが必要です。Microsoft 認証プロバイダーは、Azure サービスの使用に必要な認証に Azure Active Directory (AAD) を使用します。AAD で認証されたアカウントまたはユーザーの作成手順については、[Game Manager 用の Azure Active Directory 認証](/services/playfab/identity/dev-identity/authentication/aad-authentication) を参照してください。

アカウントまたはユーザーが Microsoft 認証プロバイダーを使用するように設定されていることを確認するには:

* [developer.playfab.com](https://developer.playfab.com) にアクセスします。
* **\[Sign in with Microsoft]** を選択して PlayFab アカウントにアクセスします。

サインインできれば、そのアカウントは Microsoft 認証プロバイダーを使用するように設定されています。

### Insights の Game Manager 権限

アカウントに、次の Game Manager 権限が有効になっている [ユーザー ロール](/services/playfab/identity/dev-identity/permissions/playfab-user-roles) を割り当てる必要があります:

* 管理者ステータス。
* Explorer タブおよび関連データへのアクセス。
* Analytics データへの読み取りおよび書き込みアクセス。

新しいユーザー ロールを作成するか、既存のロールにこれらの権限を追加することができます。

### その他の前提条件

* [Azure Active Directory (AAD) アプリケーションを作成し、タイトル データベースに接続する](/services/playfab/data-analytics/legacy/connectivity/creating-AAD-app-for-insights)

## パッケージのインストール

1. 次の NuGet パッケージをインストールします:
   * 必須: [Microsoft.Azure.Kusto.Data](https://www.nuget.org/packages/Microsoft.Azure.Kusto.Data/)
   * オプション: [Microsoft.Azure.Kusto.Ingest](https://www.nuget.org/packages/Microsoft.Azure.Kusto.Ingest/)
   * その他のオプション パッケージについては、[Kusto .NET SDK ドキュメント](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/api/netfx/about-the-sdk) を参照してください。
2. 以下は、開始するためのサンプル コードです。

```csharp theme={null}
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);
                    }
                }
            }
        }
    }
}
```

## 関連リソース

* [Azure Kusto .NET サンプル](https://github.com/Azure/azure-kusto-samples-dotnet)
* [Kusto クライアント ライブラリ ドキュメント](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/api/netfx/about-kusto-data)


## Related topics

- [外部ツールと Insights の接続](/ja-jp/services/playfab/data-analytics/legacy/connectivity/index.md)
- [Kusto Explorer を Insights に接続する](/ja-jp/services/playfab/data-analytics/legacy/connectivity/connecting-kusto-explorer-to-insights.md)
- [Python を Insights に接続する](/ja-jp/services/playfab/data-analytics/legacy/connectivity/connecting-python-to-insights.md)
- [Grafana を Insights に接続する](/ja-jp/services/playfab/data-analytics/legacy/connectivity/connecting-grafana-to-insights.md)
- [Azure Data Explorer を Insights に接続する](/ja-jp/services/playfab/data-analytics/legacy/connectivity/connecting-azure-data-explorer-to-insights.md)
