0

Problem Summary

I'm using AsgiFunctionApp to wrap FastAPI in Azure Functions, but my application logs are not appearing in the Azure Portal's Invocations tab. The logs do appear in Application Insights and Log Stream.

I know, from a different function app, that was configured differently (but I do not know how), that the logs have the Function.<function-name>.User Category, and do appear in the invocations tab.

The logs that are not showing up in the Invocations tab are categorized as Host.Function.Console instead of the expected Function.*.User category, which (I think) prevents them from showing in the Invocations tab.

Current Behavior vs Expected Behavior

Current:

  • Logs appear in Application Insights with category Host.Function.Console
  • Logs appear in Log Stream
  • Logs do NOT appear in Azure Portal > Function App > Functions > [function-name] > Invocations tab

Expected:

  • Logs should appear with category Function.<function-name>.User
  • Logs should be visible in the Invocations tab for debugging individual function executions

Environment Details

  • Azure Functions Runtime: 4.x
  • Python: 3.11
  • Framework: FastAPI with AsgiFunctionApp wrapper
  • Deployment: Container (Docker)
  • Logging Configuration: Python logging module with basicConfig

Code Structure

function_app.py

import logging
import azure.functions as func
from fastapi import FastAPI
from azure.functions import AsgiFunctionApp

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

# FastAPI app
app = FastAPI()

@app.get("/health")
async def health_check():
    logging.info("Health check endpoint called")
    return {"status": "healthy"}

@app.post("/logging/test")
async def test_logging():
    logging.info("Test logging endpoint - this should appear in Invocations")
    logging.warning("Warning level log")
    logging.error("Error level log")
    return {"message": "Logging test completed"}

# Azure Functions wrapper
function_app = AsgiFunctionApp(app=app, http_auth_level=func.AuthLevel.FUNCTION)

host.json

{
  "version": "2.0",
  "functionTimeout": "00:05:00",
  "logging": {
    "logLevel": {
      "default": "Information"
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.*, 5.0.0)"
  }
}

Attempted Solutions

  1. Different logging configurations:

    # Tried various logging setups
    logging.basicConfig(level=logging.INFO)
    
    # Tried getting function-specific logger
    logger = logging.getLogger(__name__)
    
  2. Custom logging service:

    def get_logger(name: str):
        logger = logging.getLogger(name)
        if not logger.handlers:
            handler = logging.StreamHandler()
            handler.setFormatter(logging.Formatter(
                '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
            ))
            logger.addHandler(handler)
            logger.setLevel(logging.INFO)
        return logger
    

Research Done

  • Locally the logs show up.
  • Verified that regular Azure Functions (without FastAPI/AsgiFunctionApp) do show logs in Invocations tab.
  • Confirmed logs are reaching Application Insights correctly.
  • Followed this example, which I can reproduce. The issue is however that the logs do not appear in the correct location in the azure portal.

Specific Questions

  1. How can I make logs from AsgiFunctionApp wrapped FastAPI endpoints appear in the Invocations tab?

  2. Why does AsgiFunctionApp result in Host.Function.Console categorization instead of Function.*.User?

What I would like to have

A solution that allows application logs from FastAPI endpoints (wrapped with AsgiFunctionApp) to appear in the Azure Portal's Invocations tab for individual function execution debugging, while maintaining the FastAPI framework.


0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.