Stop logging from TableClient

153 views Asked by At

I have a function app that uses a table client.

svc.AddAzureClients(bldr =>
{
    var serviceUrl = tableStorageSection["ServiceUrl"] ?? throw new ArgumentNullException("Missing value for Values:TableStorage:ServiceUrl");
    bldr.UseCredential(new DefaultAzureCredential())
        .AddTableServiceClient(new Uri(serviceUrl));
});

When I run the function locally, this client spits out a bunch of logs. enter image description here I don't want to see those, I only want the logs from my own code. However I do not seem to be able to disable it. Here's what I have in my host.json file.

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    },
    "LogLevel": {
      "Default": "Information",
      "MyApi": "Debug",
      "Azure": "None",
      "System": "None",
      "Microsoft":  "None"
    },
    "Console": {
      "IncludeScopes": false,
      "FormatterName": "MyFormatter"
    },
    "EventSource": {
      "LogLevel": {
        "Default": "None"
      }
    }
  }
}

I even created a custom formatter that outputs a logentry's category to see if the logs come from a special category, but it seems my formatter is not being used by this logger. Am I missing something here?

2

There are 2 answers

0
Stif On BEST ANSWER

I was able to tune the logging by calling the ConfigureLogging method on the HostBuilder

hostbldr.ConfigureLogging((ctx, configure) =>
{
    configure.Services.Configure<LoggerFilterOptions>(options =>
    {
        options.Rules.Add(new LoggerFilterRule("Microsoft.Azure.Functions.Worker.Logging.WorkerLoggerProvider", "Azure", LogLevel.Warning, (a, b, l) => true));
    });
});
0
Phil S On

I think your best option is to look into using an ITelemetryProcessor. This middleware intercepts ApplicationInsights telemetry and allows you to transform, filter, etc

e.g. something like this:

public class TokenExpiredFilter : ITelemetryProcessor
{
    public static readonly Type TokenExpired = typeof(SecurityTokenExpiredException);

    private ITelemetryProcessor Next { get; set; }

    public TokenExpiredFilter(ITelemetryProcessor next)
    {
        Next = next;
    }

    public void Process(ITelemetry item)
    {
        if (IsUnwantedTrace(item))
            return;

        Next.Process(item);
    }

    private static bool IsUnwantedTrace(ITelemetry item)
    {
        if (item is ExceptionTelemetry)
        {
            var exceptionTelemetry = item as ExceptionTelemetry;
            if (exceptionTelemetry.Exception.GetType() == TokenExpired)
            {
                return true;
            }
        }
        return false;
    }
}

You'll obviously need to setup the filtering to match the requirements of your issue.

Set it up in DI like this:

services.AddApplicationInsightsTelemetry();
services.AddApplicationInsightsTelemetryProcessor<TokenExpiredFilter>();