如何在 .net 核心 Web 作业中禁用 App Insights 远程依赖跟踪

How do you disable App Insights remote dependency tracking in a .net core web job

我有一个 .net core 3.0 网络作业,它使用 Microsoft.Azure.WebJobs.Logging.ApplicationInsights nuget 包将其日志记录到 Application Insights。

这一切都很棒,直到我看到我们正在生成的天蓝色账单并且日志的成本比其他任何东西都高!经过一些诊断,我发现这是由于远程依赖日志记录。

根据这篇文章记录 sql 查询和 cosmos 自动启用:

https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-dependencies

并查看该日志,我可以看到每个 sql 查询都是 运行。现在我确定在正确的情况下这对调试非常有用,但由于应用程序的性质,它 运行 有很多查询,因此导致许多我并不真正需要的日志.

我看不到的是关闭它的简单方法?

您应该在 builder.ConfigureLogging 中将 EnableDependencyTracking 设置为 false

例子:

    static void Main(string[] args)
    {
        var builder = new HostBuilder();
        builder.ConfigureWebJobs(b =>
        {
            b.AddAzureStorageCoreServices();
            b.AddAzureStorage();
        });
        builder.ConfigureLogging((context, b) =>
        {
            b.AddConsole();
            string instrumentationKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
            if (!string.IsNullOrEmpty(instrumentationKey))
            {
                //set EnableDependencyTracking to false
                b.AddApplicationInsightsWebJobs(o=> { o.InstrumentationKey = instrumentationKey;o.EnableDependencyTracking = false; });
            }
        });


        var host = builder.Build();
        using (host)
        {
            host.Run();
        }
    }