Tushar Narang
Tushar Narang

Reputation: 1957

HTTP Trigger Azure Function : OutOfMemoryException not logged in monitor

I was just going through the Azure functions, and was trying to check behavior of Azure function when exception is occurred. I went through the documentation provided by Microsoft on Error handling and made a very simple HTTP Trigger. Here is the

CODE

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log, ExecutionContext context)
{
 log.LogInformation("C# HTTP trigger function processed a request.");

 string name = req.Query["name"];

try
{
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;
    switch (name)
        {
            case "OutOfMemoryException":               
                throw new System.OutOfMemoryException();
            case "NullReferenceException":
                throw new System.NullReferenceException();
            case "IndexOutOfRangeException":
                throw new System.IndexOutOfRangeException();
            case "InvalidOperationException":
                throw new System.InvalidOperationException();
            case "ArgumentNullException":
                throw new System.ArgumentNullException();
            default:
                break;
        }
}
catch(System.Exception ex)
{
    throw;
}

return name != null
    ? (ActionResult)new OkObjectResult($"Hello, {name}")
    : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

ISSUE

I am unable to see the logs when a memory out of exception occured. Its not here and not in application insights as well, it gets logged into the console but that temp only. Inbetween the first two logs in the list, I fired an OutOfMemoryException, however there is no log of it.

Is this a known issue in Azure ?

Monitor Scrrenshot

enter image description here

Upvotes: 0

Views: 1531

Answers (2)

Jerry Liu
Jerry Liu

Reputation: 17790

OutOfMemoryException is a special exception compared to others in your list. Function host will be shut down once this exception is thrown, and no log is sent to Application Insights as you have found.

IMO, the OutOfMemoryException is usually thrown and handled by function host itself as we may have no method to handle memory problem in our own code. If you are going to set your own memory limitation, my suggestion is not to throw the exception to function host, we could log the exception by ourselves.

catch(System.Exception ex)
{
    log.LogError(ex, ex.Message);
}

We could also track all logs in kudu, go to https://<functionAppName>.scm.azurewebsites.net/DebugConsole and navigate to D:\home\LogFiles\Application\Functions\function\<FunctionName> to check function-specific logs.

Upvotes: 2

Anass Kartit
Anass Kartit

Reputation: 2088

according to this: https://learn.microsoft.com/en-us/sandbox/functions-recipes/durable-diagnostics

You have to catch the exception and log it using the logger to see it in app insight:

 catch (FunctionFailedException ex)
    {
        log.Error("Some Exception occured ", ex);
    }

Upvotes: 1

Related Questions