Nóta
Aðgangur að þessari síðu krefst heimildar. Þú getur prófað aðskrá þig inn eða breyta skráasöfnum.
Aðgangur að þessari síðu krefst heimildar. Þú getur prófað að breyta skráasöfnum.
In this tutorial, you use telemetry in your ASP.NET Core application to track feature flag evaluations and custom events. Telemetry allows you to make informed decisions about your feature management strategy. You utilize the feature flag with telemetry enabled created in the overview for enabling telemetry for feature flags. Before proceeding, ensure that you create a feature flag named Greeting in your Configuration store with telemetry enabled. This tutorial builds on top of the tutorial for using variant feature flags in an ASP.NET Core application.
Prerequisites
- The variant feature flag with telemetry enabled from Enable telemetry for feature flags.
- The application from Use variant feature flags in an ASP.NET Core application.
Add telemetry to your ASP.NET Core application
Add the latest version of the Application Insights package.
dotnet add package Microsoft.ApplicationInsights.AspNetCore dotnet add package Microsoft.FeatureManagement.Telemetry.ApplicationInsightsAdd Application Insights telemetry services to the container. Add the following code after the line that adds Azure App Configuration.
// Add Application Insights telemetry builder.Services.AddApplicationInsightsTelemetry(options => { options.ConnectionString = builder.Configuration.GetConnectionString("ApplicationInsights"); });Update the feature management services to enable telemetry publishing. Modify the existing feature management configuration to include the
AddApplicationInsightsTelemetry()call.// Add Azure App Configuration and feature management services to the container. builder.Services.AddAzureAppConfiguration() .AddFeatureManagement() .WithTargeting() .AddApplicationInsightsTelemetry();Add the targeting middleware to associate outgoing events with the targeting ID. Add the following code after the line
app.UseAzureAppConfiguration();.// Use targeting middleware to associate events with the targeting ID app.UseMiddleware<TargetingHttpContextMiddleware>();
Track custom events
Now you'll add a custom event to track when users like a quote. You'll create a backend endpoint to handle the like action and track it using Application Insights.
In Index.cshtml.cs, add a using statement for Application Insights.
using Microsoft.ApplicationInsights;Adjust the
IndexModelto get aTelemetryClient.public class IndexModel( TelemetryClient telemetry, ILogger<IndexModel> logger, IVariantFeatureManagerSnapshot featureManager ) : PageModel { private readonly TelemetryClient _telemetry = telemetry;Additionally, add a OnPostLike() function, tracking an event called "Liked" when the endpoint is used.
public IActionResult OnPostLike() { _telemetry.TrackEvent("Liked"); return new JsonResult(new { success = true }); }In Index.cshtml, add an antiforgerytoken before the script tag.
<form method="post" style="display:none;"> @Html.AntiForgeryToken() </form> <script> ...Adjust the heartClicked function with the following content.
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'); } } }The
OnPostLikemethod handles the like action. It uses theTelemetryClientto track a custom "Liked" event when a user clicks the heart button.
Build and run the app
Application Insights requires a connection string to connect to your Application Insights resource. Set the
ConnectionStrings:ApplicationInsightsas a user secret.dotnet user-secrets set ConnectionStrings:ApplicationInsights "<your-Application-Insights-Connection-String>"After setting the environment variable, restart your terminal and rebuild and run the application.
dotnet build dotnet run
Collect telemetry
Deploy your application to begin collecting telemetry from your users. To test its functionality, you can simulate user activity by creating many test users. Each user will experience one of the greeting message variants, and they can interact with the application by clicking the heart button to like a quote. As your user base grows, you can monitor the increasing volume of telemetry data collected in Azure App Configuration. Additionally, you can drill down into the data to analyze how each variant of the feature flag influences user behavior.
Additional resources
- Quote of the Day sample
- For the full feature rundown of the .NET feature management library, refer to the .NET Feature Management reference documentation.