简体   繁体   中英

How to effectively disable built-in logs in Azure Function (isolated mode) using Serilog logger?

How can I disable these logs in an Azure Function v4 isolated process?

2023-01-31T10:36:55Z   [Verbose]   Initiating background SyncTriggers operation
2023-01-31T10:37:10Z   [Verbose]   Request successfully matched the route with name '' and template 'admin/warmup'
2023-01-31T10:37:20Z   [Verbose]   Request successfully matched the route with name '' and template 'admin/warmup'
2023-01-31T10:38:38Z   [Verbose]   HttpMessageHandler expired after 120000ms for client ''
2023-01-31T10:38:48Z   [Verbose]   Starting HttpMessageHandler cleanup cycle with 1 items
2023-01-31T10:38:48Z   [Verbose]   Ending HttpMessageHandler cleanup cycle after 0.4573ms - processed: 1 items - remaining: 0 items
2023-01-31T10:45:48Z   [Verbose]   Sending invocation id:b9d8419b-26...
2023-01-31T10:52:36Z   [Information]   Executed 'Functions...
2023-01-31T10:52:36Z   [Information]   Executing 'Functions...
...

I am using Serilog. I alreadry tried setting host.json as follows:

{
    "version": "2.0",
    "logging": {
        "fileLoggingMode": "debugOnly",
        "logLevel": {
            "Function.MyFunction": "None",
            "Host.Aggregator": "None",
            "Host.Results": "None",
            "Microsoft": "None",
            "Microsoft.Hosting.Lifetime": "None",
            "Microsoft.AspNetcore": "None",
            "Microsoft.AspNetCore.Mvc.Internal": "None",
            "Microsoft.AspNetCore.Authentication": "None",
            "Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware": "None",
            "Microsoft.Extensions.Http.DefaultHttpClientFactory": "None",
            "Microsoft.Extensions.Hosting": "None",
            "System.Net.Http.HttpClient": "None",
            "Worker": "None"
        },
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            },
            "LogLevel": {
                "Default": "Warning",
                "Host.Aggregator": "None",
                "Host.Results": "None",
                "Microsoft": "None",
                "Microsoft.Hosting.Lifetime": "None",
                "Microsoft.AspNetcore": "None",
                "Microsoft.AspNetCore.Mvc.Internal": "None",
                "Microsoft.AspNetCore.Authentication": "None",
                "Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware": "None",
                "Microsoft.Extensions.Http.DefaultHttpClientFactory": "None",
                "Microsoft.Extensions.Hosting": "None",
                "System.Net.Http.HttpClient": "None",
                "Worker": "None"
            }
        }
    }
}

I also tried setting Serilog as follows in UseSerilog() method:

loggerConfiguration
    .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Error)

Also tried to set environment variables directly in Azure portal - function configuration, it did not work. It is as if the settings were ignored.
It's now been about 2 days I am looking for a solution with no luck. I saw github issues, Microsoft learn documentation, and a number of Q&A and blogs and none of these seem to work.

Does someone have a solution that works with Azure Function in isolated mode (not app service, not asp.net core)?

  • Here you can use WriteTo.Conditions to conditionally start logging to sink.

  • this WriteTo.Conditions takes two arguments the one related to the condition and the other related to the assignment of sink.

  • Here I have configured the serilog in my Program.cs file

namespace FunctionApp57
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .ConfigureServices(s => {
                        var logger = new LoggerConfiguration()
                            .Enrich.FromLogContext()
                            .WriteTo.Conditional(et => {
                                // Condition
                                if (10 == 1)
                                {
                                    return true ; 
                                }
                                else
                                {
                                    return false ; 
                                },
                                // Assignment of Sink
                                wt => wt.Console()
                    })
                .Build();
            host.Run();
        }
    }
}
  • Here the if condition return false:

在此处输入图像描述

  • Once the condition is met extra logs are logged:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM