Search code examples
azure-application-insightsazure-webjobs

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


I have a .net core 3.0 web job which is using the Microsoft.Azure.WebJobs.Logging.ApplicationInsights nuget package to log its logs to Application Insights.

This was all great until I saw the azure bill we were generating and the cost of logs was more than anything else! Following a bit of diagnosis I discovered it was due to remote dependency logging.

According to this article logging sql queries and cosmos are enabled automatically:

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

and looking at that logs I can see every sql query being run. Now I'm sure in the right circumstance this is very useful to debug with, but as the nature of the app is it runs a lot of queries which therefore is causing many gigs of logs that I don't really need.

What I can't see though is an easy way to turn it off?


Solution

  • You should set EnableDependencyTracking to false in builder.ConfigureLogging.

    The example:

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