共用方式為


啟用 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 App Configuration 的那一行後面加入以下程式碼。

    // 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>();
    

追蹤自訂事件

現在你會新增自訂事件,追蹤用戶何時按讚某句話。 你會建立一個後端端點來處理 Like 動作,並用 Application Insights 追蹤它。

  1. Index.cshtml.cs中,為應用洞察新增使用說明。

    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 中,在腳本標籤前加入防偽造代幣。

    <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 App 配置中收集的遙測資料量增加。 此外,你也可以深入分析各種功能標誌變體如何影響使用者行為。

其他資源