简体   繁体   中英

asp.net framework 4.7 configure logging level, not working

My web job is not adhering to the logging level configuration in my host.json. It is written using .NET framework 4.7. The expected result with the below code is no logging, instead, I get logging a per the below screenshot. Can any provide guidance on how to fix this? Do I need to add additional configuration/startup code?

在此处输入图片说明

host.json

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "Functions.Test": "None",
      "default": "None"
    }
  }
}

C# Web Job Code

public static void Main()

        {

            var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("host.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();

            var builder = new HostBuilder();

            builder.ConfigureWebJobs(b =>
            {
                b.AddAzureStorageCoreServices();
                b.AddTimers();
                b.AddBuiltInBindings();
            });

            builder.ConfigureLogging((context, b) =>
            {
                b.AddConsole();
            });

            var host = builder.Build();
            using (host)
            {
                var jobHost = host.Services.GetService(typeof(IJobHost)) as JobHost;
                jobHost.CallAsync("Test").GetAwaiter().GetResult();
            }
        }
    }

    public class Functions
    {
        [NoAutomaticTrigger]
        public static void Test(ILogger logger)
        {
            logger.LogCritical("LogCritical");
            logger.LogDebug("LogDebug");
            logger.LogError("LogError");
            logger.LogInformation("LogInformation");
            logger.LogTrace("LogTrace");
            logger.LogWarning("LogWarning");
            Thread.Sleep(1000 * 10);
        }
    }
}

thanks for sharing your code.

It seems to me that in the first statement a config object is being built var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder()... , however, later in the code this config variable is not used while host creation and configuration. As a result, those settings are not applied and default Information logLevel is used.

We need to supply this configuration to the HostBuilder , looks like it can be done using HostBuilder 's ConfigureAppConfiguration method. Please take a look at this question , it has an example how to use this method.

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