Microsoft Learn

Debugging Azure Functions

Debugging is a critical part of the development lifecycle. Azure Functions provides several powerful tools and techniques to help you identify and resolve issues in your serverless applications.

Local Debugging

The Azure Functions Core Tools allow you to run and debug your functions locally on your machine before deploying them to Azure. This is the most common and often the most efficient way to debug.

Using Visual Studio Code

Visual Studio Code (VS Code) with the Azure Functions extension provides an excellent debugging experience.

  1. Install the Azure Functions extension: Search for "Azure Functions" in the VS Code Extensions view and install it.
  2. Set up your project: Ensure your Azure Functions project is open in VS Code.
  3. Configure launch.json: The extension typically creates a launch.json file in a .vscode folder. This file configures debugging settings for different languages. For example, for Node.js, it might look like this:
    
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Attach to Node Functions",
                "type": "node",
                "request": "attach",
                "port": 9229,
                "preLaunchTask": "npm start"
            },
            {
                "name": "Debug: Current Function",
                "type": "node",
                "request": "launch",
                "runtimeExecutable": "npm",
                "runtimeArgs": [
                    "run",
                    "debug"
                ],
                "port": 9229
            }
        ]
    }
                        
  4. Set breakpoints: Open your function code and click in the gutter to the left of the line numbers to set breakpoints.
  5. Start debugging: Press F5 or go to the "Run and Debug" view in VS Code and select your debug configuration. The Azure Functions host will start, and your breakpoints will be hit when the function executes.

Using Visual Studio

If you're developing in C# or .NET, Visual Studio offers robust debugging capabilities integrated with Azure Functions projects.

  1. Create or open an Azure Functions project.
  2. Set breakpoints in your code.
  3. Press F5 to start debugging. Visual Studio will launch the Azure Functions host locally, and you can step through your code, inspect variables, and use the debugging tools.
Tip: Ensure you have the latest Azure Functions Core Tools installed. You can update them via npm: npm install -g azure-functions-core-tools@4 --unsafe-perm true

Remote Debugging (Azure Portal)

For debugging deployed functions, you can use the remote debugging capabilities directly from the Azure portal. This is particularly useful for understanding issues that only occur in the cloud environment.

  1. Navigate to your Azure Function App in the Azure portal.
  2. Under the "Monitoring" section, select "Log stream".
  3. Under the "Development Tools" section, select "Advanced Tools" (Kudu).
  4. In the Kudu console, navigate to Debug console > CMD or Debug console > PowerShell.
  5. Use commands to inspect logs and application state. For certain languages (like .NET), you can attach a remote debugger from Visual Studio.
Note: Remote debugging might impact the performance of your live function app. Use it judiciously.

Logging and Diagnostics

Effective logging is crucial for debugging, especially for issues that are difficult to reproduce locally.

Example Logging in JavaScript


module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? 'Hello, ' + name + '!'
        : 'This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.';

    context.log('Generated response message:', responseMessage);

    context.res = {
        status: 200,
        body: responseMessage
    };
};
            

Common Debugging Scenarios

By combining local debugging with robust logging and monitoring, you can effectively manage and troubleshoot your Azure Functions applications.