serilog-contrib / serilog-sinks-applicationinsights

A Serilog sink that writes events to Microsoft Azure Application Insights
Apache License 2.0
220 stars 72 forks source link

Skip 404/409 Blob Storage Telemetry Dependencies in AppInsights #217

Open vincenzobove opened 1 year ago

vincenzobove commented 1 year ago

Hey guys, I don't know if it's a bug but we are having some issues and would like some help. What we are trying to achieve is the following:

We have an Azure Function that make use of Serilog and this sink to write into AppInsights, this function does some calls to Azure Blob and when container/blob does not exist we do not want to log the 404/409 dependencies trace (We have so many calls that are part of the business process).

This is the startup code of the function:

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        var configuration = new ConfigurationBuilder()
                .SetBasePath(Environment.CurrentDirectory)
                .AddJsonFile("local.settings.json", true, true)
                .AddEnvironmentVariables()
                .Build();

        var loggerConfiguration = new LoggerConfiguration();

        loggerConfiguration.MinimumLevel.Warning();
        loggerConfiguration.MinimumLevel.Override("Microsoft", LogEventLevel.Error);

        loggerConfiguration.Enrich.FromLogContext();

        loggerConfiguration.WriteTo.ApplicationInsights(configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"], new TraceTelemetryConverter(), LogEventLevel.Warning);

        var logger = loggerConfiguration.CreateLogger();

        builder.Services
            .AddLogging(lb => lb.AddSerilog(logger))
            .AddApplicationInsightsTelemetryProcessor<TelemetryStorageFilterProcessor>();
    }

    public class TelemetryStorageFilterProcessor : ITelemetryProcessor
    {
        private ITelemetryProcessor Next { get; set; }
        private static readonly List<string> _dependencyTypes = new() { "azure table", "azure blob", "inproc | microsoft.storage", "microsoft.storage" };

        public TelemetryStorageFilterProcessor(ITelemetryProcessor next)
        {
            Next = next;
        }

        public void Process(ITelemetry item)
        {
            if (item is DependencyTelemetry dependency 
                && _dependencyTypes.Contains(dependency.Type.ToLower())
                && dependency.Success == false)
            {
                return;
            }

            Next.Process(item);
        }
    }
}

The Function have the following NuGet packages installed:

Microsoft.ApplicationInsights.AspNetCore 2.21.0
Serilog.AspNetCore 5.0.0
Serilog.Extensions.Logging 3.1.0
Serilog.Sinks.ApplicationInsights 3.1.0

No matter how we register the processor, the 404/409 dependencies are still written in AppInsights. What are we doing wrong?