In my previous post I looked at what we need to do to set-up and using Application Insights for our logs, but we also have access to the TelemetryClient in .NET (Microsoft also have clients for other languages etc.) and this allows us to send information to some of the other Application Insights, for example tracking events.
Tracking events is useful as a specific type of logging, i.e. we want to track, potentially, whether one of our application options is ever used, or to what extent it’s used. Imagine we have a button that runs some long running calculation – well if nobody ever uses it, maybe it’s time to deprecate and get rid of it.
Ofcourse we can just use logging for this, but the TelemetryClient allows us to capture data within the customEvents and customMetrics tables within Application Insights (we’re look at the available tables in the next post on the basics if KQL) and hence reduce the clutter of lots of logs.
Take a look at my post Logging and Application Insights with ASP.NET core. To see code for a simple test application. We’re going to simply change the app.MapGet code to look like this (note I’ve left the logging on in place as well so we can see all the options for telemetry and logging)
app.MapGet("/test", (ILogger<Program> logger, TelemetryClient telemetryClient) => { telemetryClient.TrackEvent("Test Event"); telemetryClient.TrackTrace("Test Trace"); telemetryClient.TrackException(new Exception("Test Exception")); telemetryClient.TrackMetric("Test Metric", 1); telemetryClient.TrackRequest("Test Request", DateTimeOffset.Now, TimeSpan.FromSeconds(1), "200", true); telemetryClient.TrackDependency("Test Dependency", "Test Command", DateTimeOffset.Now, TimeSpan.FromSeconds(1), true); telemetryClient.TrackAvailability("Test Availability", DateTimeOffset.Now, TimeSpan.FromSeconds(1), "Test Run", true); telemetryClient.TrackPageView("Test Page View"); logger.LogCritical("Critical Log"); logger.LogDebug("Debug Log"); logger.LogError("Error Log"); logger.LogInformation("Information Log"); logger.LogTrace("Trace Log"); logger.LogWarning("Warning Log"); }) .WithName("Test") .WithOpenApi();
As you can see, we’re injecting the TelemetryClient object and Application Insights is set up (as per my previous post) using
builder.Services.AddApplicationInsightsTelemetry(options => { options.ConnectionString = configuration["ApplicationInsights:InstrumentationKey"]; });
From the TelemetryClient we have these various “Track” methods and as you can no doubt summise, these map to
- TrackEvent: maps to the customEvents table
- TrackTrace: maps to the trace table
- TrackException: maps to the exeptions table
- TrackMetric: maps to the customMetrics table
- TrackRequest: maps to the requests table
- TrackDependency: maps to the dependencies table
- TrackAvailability: maps to the availablilityResults table
- TrackPageView: maps to the pageViews table
Telemetry along with standard logging to Application Insights gives us a wealth of information that we can look at.
Ofcourse, assuming we’re sending information to Application Insights, we’ll then want to look at features such as the Application Insights | Monitoring | Logs where we can start to query against the available tables.