1

Is there a single place where I can set the minimum severity for log writing? it should apply also to the traces written via TelemetryClient.TraceTelemetry also! there are several question covering only ILogger...

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
Leonardo
  • 10,737
  • 10
  • 62
  • 155

1 Answers1

2

There is no direct way. But you can use telemetry processor to determine which SeverityLevel can be logged.

For example, create a class which implements ITelemetryProcessor, like below:

public class MyCustomFilter : ITelemetryProcessor
{
    private ITelemetryProcessor Next { get; set; }

    // next will point to the next TelemetryProcessor in the chain.
    public MyCustomFilter(ITelemetryProcessor next)
    {
        this.Next = next;
    }

    public void Process(ITelemetry item)
    {
        TraceTelemetry traceTelemetry = item as TraceTelemetry;

        if (traceTelemetry != null)
        {
            //use this line of code to determine which SeverityLevel should be logged. In this example, only SeverityLevel is warning and up can be logged
            if (traceTelemetry.SeverityLevel < SeverityLevel.Warning) { return; }
        }

        this.Next.Process(item);
    }
}

Then register it in Startup.cs(for .net core web app):

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddApplicationInsightsTelemetry();

        //register your custom TelemetryProcessor
        services.AddApplicationInsightsTelemetryProcessor<MyCustomFilter>();
    }
Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
  • I actually figured that `services.AddApplicationInsightsTelemetryProcessor` doesn't work all around, you have to use the `aiConfig.TelemetryProcessorChainBuilder.Use(next => new SeverityLevelFilterTelemetryProcessor(next, config));` – Leonardo Sep 03 '20 at 14:55