Last active
February 19, 2020 17:26
-
-
Save hd9/d2aeed566504256255c4a98561908baa to your computer and use it in GitHub Desktop.
Suppressing Application Insights telemetry on an ASP.NET Core web applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ********************************************** | |
// 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