Application Insights is a wonderful tool. Especially when you have a microservice or multi-application environment and you need one place for all the logs and metrics. But it’s not free, and the costs can run wild if you are not careful.
Although the message logging usually is the most expensive cost, remote dependencies and requests can take up a lot of the costs too.
You can suppress the telemetry data by using a ITelemetryProcessor. The ITelemetryProcessor processes the telemetry information before it is send to Application Insights, and can be useful in many situations, including as a filter.
Take a look at this graph, the red part are my dependencies, and you can see the drop in tracking after the filter was applied:
This is an example on the dependency telemetry filter that will exclude all successful dependencies to be tracked, but allow all those that fails:
using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.Extensibility; namespace MyCode { public class DependencyTelemetryFilter : ITelemetryProcessor { private readonly ITelemetryProcessor _nextProcessor; public DependencyTelemetryFilter(ITelemetryProcessor nextProcessor) { _nextProcessor = nextProcessor; } public void Process(ITelemetry telemetry) { if (telemetry is DependencyTelemetry dependencyTelemetry) { if (dependencyTelemetry.Success == true) { return; } } _nextProcessor.Process(telemetry); } } }
To add the filter, simply call the AddApplicationInsightsTelemetryProcessor method in your startup code:
private void ConfigureApplicationInsights(IServiceCollection services) { services.AddApplicationInsightsTelemetryProcessor<DependencyTelemetryFilter>(); }
MORE TO READ:
- What is Application Insights? from microsoft
- Dependency Tracking in Azure Application Insights from microsoft
- How dependency calls are tracked by the Application Insights by lowleveldesign.org
- How to limit Azure Application Insights to only log custom events in dotnet core project? from stackoverflow
- How to suppress Application Insights telemetry by hildenco software
Pingback: C# Log to Application Insights and File from your .NET 6 Application | Brian Pedersen's Sitecore and .NET Blog