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
...
Asked
Active
Viewed 602 times
1

Ivan Glasenberg
- 29,865
- 2
- 44
- 60

Leonardo
- 10,737
- 10
- 62
- 155
-
what's your project? .net or .net core? web or console project? – Ivan Glasenberg Sep 03 '20 at 00:33
-
@IvanYang .net core, 3.1, latest SDK, both console and web – Leonardo Sep 03 '20 at 00:53
-
Just confirm, do you actually mean log level? like just capture the log level like information or trace or exception? – Ivan Glasenberg Sep 03 '20 at 01:27
-
I mean `SeverityLevel`. Say that I don't want to register Verboses logs anymore, only warnings and up – Leonardo Sep 03 '20 at 01:46
1 Answers
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