你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

为 ASP.NET Core 应用程序中的功能标志启用遥测

在本教程中,你将在 ASP.NET Core 应用程序中使用遥测来跟踪功能标志评估和自定义事件。 遥测允许你对功能管理策略做出明智的决策。 利用在为功能标志启用遥测概述中创建的、已启用遥测的功能标志。 在继续作之前,请确保在配置存储中创建一个名为 Greeting 的功能标志,并启用遥测。 本教程基于在 ASP.NET Core 应用程序中使用变体功能标志的教程进行扩展。

先决条件

将遥测添加到 ASP.NET Core 应用程序

  1. 添加最新版本的 Application Insights 包。

    dotnet add package Microsoft.ApplicationInsights.AspNetCore
    dotnet add package Microsoft.FeatureManagement.Telemetry.ApplicationInsights
    
  2. 将 Application Insights 遥测服务添加到容器。 在添加 Azure 应用配置的行后面添加以下代码。

    // Add Application Insights telemetry
    builder.Services.AddApplicationInsightsTelemetry(options =>
    {
        options.ConnectionString = builder.Configuration.GetConnectionString("ApplicationInsights");
    });
    
  3. 更新功能管理服务以启用遥测发布。 修改现有功能管理配置以包括 AddApplicationInsightsTelemetry() 调用。

    // Add Azure App Configuration and feature management services to the container.
    builder.Services.AddAzureAppConfiguration()
        .AddFeatureManagement()
        .WithTargeting()
        .AddApplicationInsightsTelemetry();
    
  4. 添加目标中间件以将传出事件与目标 ID 相关联。 在行 app.UseAzureAppConfiguration();后添加以下代码。

    // Use targeting middleware to associate events with the targeting ID
    app.UseMiddleware<TargetingHttpContextMiddleware>();
    

跟踪自定义事件

现在,你将添加一个自定义事件,用于跟踪用户点赞某条引用内容的情况。 你将创建一个后端终结点来处理点赞操作,并使用 Application Insights 对其进行跟踪。

  1. Index.cshtml.cs中,为 Application Insights 添加 using 语句。

    using Microsoft.ApplicationInsights;
    
  2. 调整IndexModel以获取一个TelemetryClient

    public class IndexModel(
            TelemetryClient telemetry,
            ILogger<IndexModel> logger,
            IVariantFeatureManagerSnapshot featureManager
        ) : PageModel
    {
        private readonly TelemetryClient _telemetry = telemetry;
    
  3. 此外,添加 OnPostLike() 函数,在使用终结点时跟踪名为“Liked”的事件。

    public IActionResult OnPostLike()
    {
        _telemetry.TrackEvent("Liked");
        return new JsonResult(new { success = true });
    }
    
  4. Index.cshtml 中,在脚本标记之前添加 antiforgerytoken。

    <form method="post" style="display:none;">
        @Html.AntiForgeryToken()
    </form>
    
    <script>
        ...
    
  5. 使用以下内容调整 heartClicked 函数。

    async function heartClicked(button) {
        const response = await fetch(`?handler=Like`, { method: "POST" });
        if (response.ok) {
            const data = await response.json();
            if (data.success) {
                var icon = button.querySelector('i');
                icon.classList.toggle('far');
                icon.classList.toggle('fas');
            }
        }
    }
    

    OnPostLike方法处理点赞操作。 TelemetryClient 用于在用户单击心形按钮时,跟踪自定义的“赞”事件。

生成并运行应用

  1. Application Insights 需要使用连接字符串才能连接到 Application Insights 资源。 将 ConnectionStrings:ApplicationInsights 设置为用户机密信息。

    dotnet user-secrets set ConnectionStrings:ApplicationInsights "<your-Application-Insights-Connection-String>"
    
  2. 设置环境变量后,重启终端并重新生成并运行应用程序。

    dotnet build
    dotnet run
    

收集遥测数据

部署应用程序以开始从用户收集遥测数据。 若要测试其功能,可以通过创建多个测试用户来模拟用户活动。 每个用户都将接收到其中一个问候消息的变体,他们可以通过点击心形按钮与应用程序交互,为某个句子点赞。 随着用户群的增长,可以监视 Azure 应用配置中收集的遥测数据量增加。 此外,还可以向下钻取到数据,以分析功能标志的每个变体如何影响用户行为。

其他资源