We filter out just hangfire SQL using a telemetry processor like this, in a .net5 asp.net project. Note that we use a different database for Hangfire so the telemetry processor can easily out hangfire SQL by checking if it's connecting to the Hangfire database.
Within Startup.Configure():
var hangFireConnectionString = // ... get from somewhere
configuration.TelemetryProcessorChainBuilder
.Use(next => new IgnoreHangfireTelemetry(next,hangFireConnectionString))
.Build();
Here's the processor class:
public class IgnoreHangfireTelemetry : ITelemetryProcessor
{
private readonly ITelemetryProcessor next;
private readonly string hangfireDashboardPath;
private readonly string sqlDatabase; // name of the hangfire db
public IgnoreHangfireTelemetry(
ITelemetryProcessor next,
string sqlConnectionString = null,
string hangfireDashboardPath = "/hangfire")
{
this.next = next ?? throw new ArgumentNullException(nameof(next));
if (!string.IsNullOrEmpty(sqlConnectionString))
{
var builder = new SqlConnectionStringBuilder(sqlConnectionString);
sqlDatabase = builder.InitialCatalog;
}
this.hangfireDashboardPath = hangfireDashboardPath ?? throw new ArgumentNullException(nameof(hangfireDashboardPath));
}
public void Process(ITelemetry item)
{
var request = item as RequestTelemetry;
// If it's a request for Hangfire Dashboard don't record it
if (request != null
&& request.Url.AbsolutePath.StartsWith(hangfireDashboardPath))
{
return;
}
var telemetry = item as DependencyTelemetry;
// If it's a SQL dependency to the Hangfire db don't record it
if (telemetry != null)
{
if (sqlDatabase != null && telemetry.Type == "SQL"
&& telemetry.Target.EndsWith($"| {sqlDatabase}", StringComparison.OrdinalIgnoreCase))
{
return;
}
if (telemetry.Type == "SQL"
&& telemetry.Name.ToLower().Contains("hangfire")
&& telemetry.Success.GetValueOrDefault(false))
{
return;
}
}
// Looks like it's not Hangfire, process the telemetry as usual.
next.Process(item);
}
}
If you don't use a separate Hangfire db, you could achieve the same thing by inspecting other DependencyTelemetry properties, e.g. look at DependencyTelemetry.Data or .CommandName (which holds the SQL statement) and check if it contains [Hangfire] (or another db schema name if you've changed Hangfire to use a different schema). If you're just filtering on sql you'll need to filter out a few more commands. Just step through using the debugger and see which ones you need to exclude. For example, in above code my sqlBackend object looks like this (note that "." is my SQL Server name, i.e. running locally, and "Hangfire" is the DB name where my Hangfire tables are):

Sometimes the sqlBackend.Name property has values like . | Hangfire | Open, or . | Hangfire | sp_getapplock, but the Target property is still just ``. | Hangfire` e.g.:
