【问题标题】:Application Insights - Global Minimum Severity Level Filter?Application Insights - 全球最低严重性级别筛选器?
【发布时间】:2020-09-02 20:38:48
【问题描述】:

是否有一个地方可以设置日志写入的最低严重性?它也应该适用于通过TelemetryClient.TraceTelemetry 写的痕迹!有几个问题只涉及ILogger...

【问题讨论】:

  • 你的项目是什么? .net 还是 .net 核心?网络或控制台项目?
  • @IvanYang .net core, 3.1, 最新的SDK,控制台和网页版
  • 确认一下,你说的是日志级别吗?就像只捕获信息或跟踪或异常等日志级别?
  • 我的意思是SeverityLevel。说我不想再注册 Verboses 日志了,只有警告和向上

标签: c# .net azure azure-application-insights


【解决方案1】:

没有直接的方法。但是您可以使用telemetry processor 来确定可以记录哪个SeverityLevel

例如,创建一个实现 ITelemetryProcessor 的类,如下所示:

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);
    }
}

然后在Startup.cs中注册(对于.net core web app):

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

        //register your custom TelemetryProcessor
        services.AddApplicationInsightsTelemetryProcessor<MyCustomFilter>();
    }

【讨论】:

  • 我实际上认为services.AddApplicationInsightsTelemetryProcessor 并不能解决所有问题,您必须使用aiConfig.TelemetryProcessorChainBuilder.Use(next =&gt; new SeverityLevelFilterTelemetryProcessor(next, config));
猜你喜欢
  • 2015-08-19
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 2019-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多