Search code examples
.netazureloggingazure-application-insights

Filtering out logged statement


I'm looking for a way to reduce the number of logged statements sent to Application Insights. While .NET logging allows to filter out logs using LogLevels and categories, there's nothing that would allow removing log entries based on the logged content. Filter function can only operate on the provider name, category, and LogLevel.

Is this possible?


Solution

  • There are a couple of methods you can use. First of all, Application Insights (AI) allows you to apply sampling. It is even turned on by default in most cases. It does not, however ,allow you to apply sampling based on content. It only allows you to specify what kind of telemetry is subject to sampling.

    I think the best fit for you is using an ITelemetryProcessor implementation. See the docs

    All telemetry send to AI, whether it is auto collected or manually send to AI, will pass the filter and based on the content of the telemetry item being passed you can choose to drop it entirely.

    Do mind this operates on a different level that the .Net logging mechanism. This method only applies filtering of the data send to AI, so if you have set up your logger to send logs to different sinks those won't be affected.

    Sample implementation (from the docs):

    using Microsoft.ApplicationInsights.Channel;
    using Microsoft.ApplicationInsights.Extensibility;
    
    public class SuccessfulDependencyFilter : ITelemetryProcessor
    {
        private ITelemetryProcessor Next { get; set; }
    
        // next will point to the next TelemetryProcessor in the chain.
        public SuccessfulDependencyFilter(ITelemetryProcessor next)
        {
            this.Next = next;
        }
    
        public void Process(ITelemetry item)
        {
            // To filter out an item, return without calling the next processor.
            if (!OKtoSend(item)) { return; }
    
            this.Next.Process(item);
        }
    
        // Example: replace with your own criteria.
        private bool OKtoSend (ITelemetry item)
        {
            var dependency = item as DependencyTelemetry;
            if (dependency == null) return true;
    
            return dependency.Success != true;
        }
    }