Search code examples
c#.netazureloggingazure-functions-isolated

.NET 8 ISOLATED upgrade stopped logging Infomation, Debug


I recently upgraded our Azure Functions app from .NET 6 (in-progress model) to .NET 8 (isolated worker model). Some strange behavior is occuring where I'm unable to see Information (or below) level logs. The settings pertaining to logs are the same; everything worked in .NET 6. I'm trying to see these logs locally in my console.

Code:

Logger?.LogError("Test if we can log error");
Logger?.LogWarning("Test if we can log warning");
Logger?.LogInformation("Test if we can log info");
Logger?.LogDebug("Test if we can log debug");

Logs:

[2024-05-30T10:24:45.823Z] Test if we can log error
[2024-05-30T10:24:45.824Z] Test if we can log warning
.. that's it

Logging obviously works because I can see Error and Warning log levels. My host.json:

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "AlarmFunctions.Functions": "Debug",
      "DataLayer": "Debug",
      "ServiceLayer": "Debug",
      "Function": "Warning",
      "default": "Warning"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Trace;Request"
      },
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

Important to note is that logLevel in host.json does not apply anymore. Above provided logging code is located in namespace AlarmFunctions.Functions.SomeClassName but changing loglevel to "AlarmFunctions.Functions": "Error" still makes the warning log show up.

I tried changing ApplicationInsights log level as well but it doesn't change anything. I also tried this but it doesn't work either.

Am i missing something?

Thanks


Solution

  • If you are looking to set log levels specific to Functions. You need to add filter in Program.cs file.

    My Function:

    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Logging;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Azure.Functions.Worker.Http;
    using System.Threading.Tasks;
    
    namespace FunctionApp12
    {
        public class Function1
        {
            private readonly ILogger<Function1> _logger;
            public Function1 (ILogger<Function1> logger)
            {
                _logger = logger;
            }
            [Function("Function1")]
            public IActionResult Run(
                [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req)
            {
                _logger?.LogCritical("Critical log");
                _logger?.LogWarning("Warning log");
                _logger?.LogError("Error Log");
                _logger?.LogInformation("Information log");
    
                var responseMessage = "Welcome to Azure Functions";
    
               return new OkObjectResult(responseMessage);
            }
        }
    }
    
    

    FunctionApp12.csproj:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <AzureFunctionsVersion>v4</AzureFunctionsVersion>
        <OutputType>Exe</OutputType>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
      <ItemGroup>
        <FrameworkReference Include="Microsoft.AspNetCore.App" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.3.1" />
        <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />  
      </ItemGroup>
      <ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
      </ItemGroup>
    </Project>
    

    Program.cs:

    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    using Microsoft.Azure.Functions.Worker;
    
    var host = new HostBuilder()
        .ConfigureFunctionsWebApplication()
        .ConfigureServices(services =>
        {
            services.AddApplicationInsightsTelemetryWorkerService();
            services.ConfigureFunctionsApplicationInsights();
        })
        .ConfigureLogging((context, logging) =>
        {
           logging.AddFilter("FunctionApp12.Function1", LogLevel.Warning);
        })
        .Build();
    
    host.Run();
    

    host.json:

    {
        "version": "2.0",
      "logging": {
        "logLevel": {
          "default": "Debug"
        },
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          },
          "enableLiveMetricsFilters": true
        }
      }
    }
    

    OUTPUT: