Azure Functions - .NET 8 isolated - logging no longer showing up in Azure Portal Invocation Traces

2 min read 04-10-2024
Azure Functions - .NET 8 isolated - logging no longer showing up in Azure Portal Invocation Traces


Azure Functions: Missing Logs in .NET 8 Isolated - What's Going On?

The Problem:

You're running your Azure Function in the .NET 8 isolated process model and find that your logs, previously visible in the Azure Portal's Invocation Traces, are missing. This can be a frustrating experience, as it makes debugging and troubleshooting significantly more difficult.

Scenario:

Let's imagine you have a simple Azure Function written in .NET 8 using the isolated process model:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace MyFunctionApp
{
    public class MyFunction
    {
        private readonly ILogger<MyFunction> _logger;

        public MyFunction(ILogger<MyFunction> logger)
        {
            _logger = logger;
        }

        [FunctionName("MyFunction")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
            ILogger log)
        {
            _logger.LogInformation("C# HTTP trigger function processed a request.");
            log.LogInformation("Another log message.");

            return new OkObjectResult("This HTTP triggered function executed successfully.");
        }
    }
}

You deploy this function to Azure, but upon execution, you find no logs in the Azure Portal's Invocation Traces.

Understanding the Issue:

The missing logs are a consequence of the changes introduced in the .NET 8 isolated process model. This new model provides improved performance and isolation, but it also brings about changes in how logging is handled. The ILogger instances available within your function code are no longer automatically configured to send their logs to the Azure Portal's Invocation Traces.

The Solution:

To restore your logging visibility, you need to configure the logging provider to send your logs to the Azure Portal. This is typically achieved by configuring the Azure.Functions.Worker.Logging package in your function's project.

Steps to Resolve:

  1. Install the Azure.Functions.Worker.Logging package:

    dotnet add package Azure.Functions.Worker.Logging
    
  2. Configure the logging provider in your Program.cs:

    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add logging configuration
    builder.Logging.AddConsole();
    builder.Logging.AddDebug();
    builder.Logging.AddAzureWebAppDiagnostics(); // This is the key!
    
    var app = builder.Build();
    
    // ... rest of your app configuration ...
    

    The line builder.Logging.AddAzureWebAppDiagnostics(); is crucial. It enables the logging provider to send your logs to the Azure Portal.

  3. Rebuild and redeploy your function:

    Make sure to rebuild your function after the changes and redeploy it to Azure for the new logging configuration to take effect.

Further Considerations:

  • Log Level: You can control the level of logging (e.g., Information, Warning, Error) using the LogLevel enum in your AddAzureWebAppDiagnostics() call.

  • Log Rotation: For better log management, consider setting up log rotation policies within your Azure Function App to prevent log files from growing indefinitely.

Key Takeaways:

  • The .NET 8 isolated process model requires explicit configuration of logging providers to ensure logs are sent to the Azure Portal.
  • The Azure.Functions.Worker.Logging package and AddAzureWebAppDiagnostics() are essential for logging to the Azure Portal in .NET 8 isolated functions.

Resources:

By implementing these solutions, you'll regain visibility into your Azure Function logs, facilitating efficient debugging and troubleshooting within the .NET 8 isolated process model.