Skip to content

Instantly share code, notes, and snippets.

@hd9
Last active February 19, 2020 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hd9/d2aeed566504256255c4a98561908baa to your computer and use it in GitHub Desktop.
Save hd9/d2aeed566504256255c4a98561908baa to your computer and use it in GitHub Desktop.
Suppressing Application Insights telemetry on an ASP.NET Core web applications
// **********************************************
// Fore more information, visit:
// https://blog.hildenco.com/2020/02/suppressing-application-insights.html
// **********************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace aspnet_ai.Infrastructure.Filters
{
public class SuppressStaticResourcesFilter : ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
// some of the static resources that I'd like to exclude from my telemetry
static readonly List<string> names = new List<string> { "favicon.ico", "bootstrap", "jquery", "site.css", "site.js" };
// next will point to the next TelemetryProcessor in the chain.
public SuppressStaticResourcesFilter(ITelemetryProcessor next)
{
Next = next;
}
public void Process(ITelemetry item)
{
// To exclude requests from our telemetry we sould use RequestTelemetry
// For dependencies, use DependencyTelemetry
var req = item as RequestTelemetry;
if (req != null && names.Any(n => req.Name.Contains(n)))
{
return;
}
// Send everything else
this.Next.Process(item);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment