Getting customEvents with Azure Application Insights customEvents and .NET 6
dotnet new mvc -o videowebapp cd videowebapp dotnet add package Microsoft.ApplicationInsights.AspNetCore
Program.cs: Adding builder.Services.AddApplicationInsightsTelemetry();
var builder = WebApplication.CreateBuilder(args); // Add services to the container. IServiceCollection serviceCollection = builder.Services.AddApplicationInsightsTelemetry(); builder.Services.AddControllersWithViews(); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run();
Getting InstrumentationKey
az resource show \ --resource-group <resource_group_name> \ --name <resource_name> \ --resource-type "Microsoft.Insights/components" \ --query properties.InstrumentationKey
appsettings.json : adding InstrumentationKey
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "ApplicationInsights": { "InstrumentationKey": "0000000000000000000000000000000", "LogLevel": { "Default": "Information", "Microsoft": "Error" } } }
HomeController.cs : Adding ” this.aiClient.TrackEvent(“CommentSubmitted”); ”
using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using videowebapp.Models; using Microsoft.ApplicationInsights; namespace videowebapp.Controllers; public class HomeController : Controller { private TelemetryClient aiClient; // private readonly ILogger<HomeController> _logger; public HomeController(TelemetryClient aiClient) { this.aiClient = aiClient; } /* public HomeController(ILogger<HomeController> logger) { _logger = logger; } */ public IActionResult Index() { // _logger.LogWarning("fujuTE: An example of a Warning trace.."); // _logger.LogError("fujuTE: An example of an Error level message"); return View(); } public IActionResult Privacy() { // _logger.LogInformation("fujuTE: An example of a Information trace.."); // Track an event this.aiClient.TrackEvent("CommentSubmitted"); // Track an event with properties this.aiClient.TrackEvent("VideoUploaded", new Dictionary<string, string> {{"Category", "Sports"}, {"Format", "mp4"}}); return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } }


See Also:
- https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net\
- https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-trace-logs
- https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.trace.traceinformation?view=net-6.0
- https://docs.microsoft.com/en-us/azure/azure-monitor/app/tutorial-app-dashboards
- https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/nullable-reference-types
- https://github.com/dotnet/samples
- https://docs.microsoft.com/en-us/learn/modules/instrument-web-app-code-with-application-insights/
- https://learn.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string?tabs=net