0

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?

enter image description here

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);
        }
    }
}
user3845056
  • 489
  • 7
  • 25

1 Answers1

0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
ochzhen
  • 156
  • 2
  • 5
  • Thanks for the info, unfortunately, I get the same result when using this code instead. Any other thoughts? ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; var builder = new HostBuilder(); builder.ConfigureAppConfiguration(cb => cb // .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("host.json") ); – user3845056 Nov 24 '20 at 05:22
  • One thing is to double check that it actually finds `host.json`, you can specify `optional: false` in `AddJsonFile` method – ochzhen Nov 24 '20 at 05:48
  • I ran process mon to confirm the host.json file is being opened. as well as setting optional: false. Do you have any other suggestions? – user3845056 Nov 24 '20 at 14:08
  • We can try to rename `host.json` to `appsettings.json` since `ConfigureWebJobs` adds this config file by default. Also, we can try to change the order of `ConfigureWebJobs` and `ConfigureAppConfiguration`, looks like `ConfigureWebJobs` should go first. Please take a look at this issue: https://github.com/Azure/azure-webjobs-sdk/issues/1931 – ochzhen Nov 24 '20 at 16:00
  • If I add the b.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); line to configure logging method then I get trace level logging. I have confirmed that the host.json file is being loaded, so my theory is the host.json file is not applied for .net Framework 4.7 web jobs or the host.json file is not correctly formatted. Does anyone have any ideas/suggestions? – user3845056 Nov 29 '20 at 09:12
  • Have you had a chance to check out the github issue shared above? If yes, please let me know why it's not applicable to your case. – ochzhen Nov 29 '20 at 17:07
  • Yes I also rearranged the code to have ConfigureWebJob execute first followed by ConfigureAppConfiguration. I also renamed the host.json to appsettings.json – user3845056 Nov 30 '20 at 01:29