> ## 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) 连接](/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 账户

你需要一个 PlayFab 账户或用户，其身份验证提供商设置为 Microsoft。Microsoft 身份验证提供商使用 Azure Active Directory (AAD) 进行身份验证，这是使用 Azure 服务所必需的。有关创建 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 连接](/zh-CN/services/playfab/data-analytics/legacy/connectivity/index.md)
- [将 Kusto Explorer 连接到 Insights](/zh-CN/services/playfab/data-analytics/legacy/connectivity/connecting-kusto-explorer-to-insights.md)
- [将 Python 连接到 Insights](/zh-CN/services/playfab/data-analytics/legacy/connectivity/connecting-python-to-insights.md)
- [将 Grafana 连接到 Insights](/zh-CN/services/playfab/data-analytics/legacy/connectivity/connecting-grafana-to-insights.md)
- [将 Azure Data Explorer 连接到 Insights](/zh-CN/services/playfab/data-analytics/legacy/connectivity/connecting-azure-data-explorer-to-insights.md)
