QTom
QTom

Reputation: 1521

Azure Function - LogLevel via host.json not working correctly

I'm making an Azure Function app in .NET 8.

I'm trying to configure the logging level via the host.json file however it does not work as expected. I'm trying to very simply set the default level to Warning and then our root namespace to Information so that I see information logs from our code and only warnings from anything else:

"logLevel": {
  "default": "Warning",
  "Root.Namespace": "Information"
},

When I add this, the logging does change, so something is happening, however my information logs do not ever show up. I am just running locally and looking at the console output.

When I configure this in Program.cs like so instead:

.ConfigureLogging(logging =>
{
    logging.Services.Configure<LoggerFilterOptions>(options =>
    {
        options.AddFilter("Default", LogLevel.Warning);
        options.AddFilter("Root.Namespace", LogLevel.Information);
    });
})

This works fine.

What is going on? I am logging in a namespace descending from "Root.Namespace" i.e. "Root.Namespace.Some.Thing.Else"

Everything I read says this should work?

Edit: If I have it in both Program.cs and host.json it also does not work, so it seems host.json is overwriting it.

Upvotes: 1

Views: 157

Answers (1)

RithwikBojja
RithwikBojja

Reputation: 11253

When I add this, the logging does change, so something is happening, however my information logs do not ever show up.

In Azure Functions you have to use Function.FunctionName not as namespace. Then the logs will appear:

host.json:

{
    "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      },
      "enableLiveMetricsFilters": true
    },
    "default": "Warning",
    "Function.Function1": "Information"
  }
}

Function.cs:

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace FunctionApp6
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest tst,
            ILogger ri_lger)
        {
            ri_lger.LogInformation("Hello Rithwik Bojja, How are You!!");
            string ri_out = "Hello Rithwik";
            return new OkObjectResult(ri_out);
        }
    }
}

Output:

enter image description here

Alternatively it will also work with below host.json (For multiple functions, one do not need to add all function names):

In this i have used Functionappname.Function :

{
    "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      },
      "enableLiveMetricsFilters": true
    },
    "default": "Warning",
    "FunctionApp6.Function": "Information"
  }
}

For more information refer my answer in SO-Thread .

Upvotes: 0

Related Questions