Skip to content

Instantly share code, notes, and snippets.

@gbellmann
Created January 29, 2017 17:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbellmann/fcb146a92bb93a33cb4d8e92ce361a85 to your computer and use it in GitHub Desktop.
Save gbellmann/fcb146a92bb93a33cb4d8e92ce361a85 to your computer and use it in GitHub Desktop.
Telemetry processor to ignore certain 404 errors in Application Insights
using System;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace Samples
{
public class My404Filter : ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
public My404Filter(ITelemetryProcessor next)
{
Next = next;
}
public void Process(ITelemetry item)
{
// To filter out an item, just return
if (ShouldIgnoreRequest(item))
{
return;
}
Next.Process(item);
}
private static bool ShouldIgnoreRequest(ITelemetry item)
{
if (item.Context.Operation.Name != null)
{
var operationName = item.Context.Operation.Name.ToLower();
if (operationName.StartsWith("get api/getobjectwith404s"))
{
var req = item as RequestTelemetry;
return req != null && req.ResponseCode.Equals("404", StringComparison.OrdinalIgnoreCase);
}
return false;
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment