Turn off logging for one Azure function

Richard Barraclough 6 Reputation points
2024-04-24T11:36:31.81+00:00

I really need to turn off logging for the health check function in my Azure functions app because it's just spamming ApplicationInsights with noise.

The documentation at https://learn.microsoft.com/en-us/azure/azure-functions/functions-host-json implies that this is something to do with the logging.logLevel object in the host.json file, but it's unclear what to put on the left hand side of the :.

This is what I've got. The only thing that has any effect is the Host.Results that makes all logging stop.

  "logging": {
    "logLevel": {
      "default": "Trace",
      "System": "Warning",
      "Microsoft": "Warning",
      "MyCompany.CalculationsService.Functions.HealthFunction": "Warning",
      "MyCompany.CalculationsService.Functions.HealthFunction.Health": "Warning",
      "Functions.Health": "Warning",
      "Functions.Health.User": "Warning",
      "Function.Health": "Warning",
      "Function.Health.User": "Warning",
      "Health": "Warning",
      //"Host.Results": "Warning" // This turns off everything.
    },

Is this even possile at all? Is it just all or nothing?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,301 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Sreeju Nair 11,616 Reputation points
    2024-04-25T06:53:13.18+00:00

    Did you try changing the default logLevel to Warning, as trace contains the most detailed information?

    Refer: https://www.microsoftazuresponsorships.com/Balance#

    I can see the log Category configuration needs to be modified. You can see the details of how to configure these categories from the below URL.

    https://learn.microsoft.com/en-us/azure/azure-functions/configure-monitoring?tabs=v2#configure-categories

    Hope this helps


  2. Richard Barraclough 6 Reputation points
    2024-04-25T10:27:56.4+00:00

    This seems to do it

        internal class NoHealthTelemetryInitializer : ITelemetryInitializer
        {
            public void Initialize(ITelemetry item)
            {
                ISupportProperties? tcp = item as ISupportProperties;
                if(tcp.Properties.Any(z => z.Key == "AzureFunctions_FunctionName" && z.Value == "Health"))
                {
                    // We don't want to track health checks in the metrics.
                    if (item is ISupportAdvancedSampling advancedSampling)
                    {
                        advancedSampling.ProactiveSamplingDecision = SamplingDecision.SampledOut;
                    }
                    // For the case that we cannot filter out the telemetry, we mark it as synthetic
                    if (string.IsNullOrWhiteSpace(item.Context.Operation.SyntheticSource))
                    {
                        item.Context.Operation.SyntheticSource = "Health";
                    }
                }
            }
        }
    
    
    0 comments No comments

  3. Richard Barraclough 6 Reputation points
    2024-04-25T10:28:27.84+00:00

    This seems to do it.

        internal class NoHealthTelemetryInitializer : ITelemetryInitializer
        {
            public void Initialize(ITelemetry item)
            {
                ISupportProperties? tcp = item as ISupportProperties;
                if(tcp.Properties.Any(z => z.Key == "AzureFunctions_FunctionName" && z.Value == "Health"))
                {
                    // We don't want to track health checks in the metrics.
                    if (item is ISupportAdvancedSampling advancedSampling)
                    {
                        advancedSampling.ProactiveSamplingDecision = SamplingDecision.SampledOut;
                    }
                    // For the case that we cannot filter out the telemetry, we mark it as synthetic
                    if (string.IsNullOrWhiteSpace(item.Context.Operation.SyntheticSource))
                    {
                        item.Context.Operation.SyntheticSource = "Health";
                    }
                }
            }
        }
    
    
    0 comments No comments