【问题标题】:Is it possible to disable application insights tracking in a specific web api controller?是否可以在特定的 Web api 控制器中禁用应用程序洞察跟踪?
【发布时间】:2020-05-08 18:36:14
【问题描述】:

我有一个 Asp.net 核心 web api 项目,我在 Startup.cs 文件中添加了最简单的应用程序洞察配置。

 services.AddApplicationInsightsTelemetry();

一切都很好,数据已正确发送到 Azure,但现在我需要停止跟踪来自特定控制器的数据,因为它向外部 api 发出请求,当它失败时我能够控制流,但我不想在应用程序洞察门户中看到那些错误。

【问题讨论】:

  • 您好,如果回答对您有帮助,您能否按照link 接受它作为回答?谢谢。

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


【解决方案1】:

您可以使用telemetry processor过滤掉指定控制器的数据,示例代码如下:

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

    public CustomControllerFilter(ITelemetryProcessor next)
    {
        this.Next = next;
    }
    public void Process(ITelemetry item)
    {
        RequestTelemetry telemetry = item as RequestTelemetry;

        if (telemetry != null)
        {
            //the test controller
            if (telemetry.Url.AbsoluteUri.Contains("/test/"))
            {
                return;
            }
        }


        this.Next.Process(item);
    }
}

然后在 Startup.cs -> ConfigureServices 方法中注册:

    public void ConfigureServices(IServiceCollection services)
    {
        //other code
        services.AddApplicationInsightsTelemetry();

        //register it here
        services.AddApplicationInsightsTelemetryProcessor<CustomControllerFilter>();
    }

【讨论】:

    【解决方案2】:

    您可以创建一个方法来禁用它并调用控制器。该方法将包括:

    TelemetryConfiguration.Active.DisableTelemetry = true;
    

    另一个选项是修改 appsettings.json 以控制 ApplicationInsights 完成的日志记录。 appsettings.json 中的以下内容确保 Applicationinsights 仅发送严重性 >= 警告的日志的跟踪。

    {
      "Logging": {
        "IncludeScopes": false,
        "Debug": {
          "LogLevel": {
            "Default": "Trace"
          }
        },
        "Console": {
          "LogLevel": {
            "Default": "Warning"
          }
        },
        "ApplicationInsights": {
          "LogLevel": {
            "Default": "Warning"
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 2015-11-10
      • 2018-08-30
      相关资源
      最近更新 更多