我只是浏览Azure函数,并试图在发生异常时检查Azure函数的行为。我浏览了Microsoft提供的有关错误处理的文档,并做了一个非常简单的HTTP触发器。这是
代码
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");
}
问题
发生内存异常时,我看不到日志。它不在这里,也不在应用程序见解中,它只登录到控制台,而是临时登录。在列表的前两个日志之间,我触发了OutOfMemoryException,但是没有日志。
这是Azure中的已知问题吗?
答案 0 :(得分:1)
OutOfMemoryException
是一个特殊的例外。一旦引发此异常,功能主机将关闭,并且不会像发现的那样将日志发送到Application Insights。
IMO,OutOfMemoryException
通常由函数主机本身抛出和处理,因为我们可能没有方法来处理自己代码中的内存问题。如果您要设置自己的内存限制,我的建议是不要将异常抛出给函数主机,我们可以自己记录该异常。
catch(System.Exception ex)
{
log.LogError(ex, ex.Message);
}
我们还可以跟踪kudu中的所有日志,转到https://<functionAppName>.scm.azurewebsites.net/DebugConsole
,然后导航到D:\home\LogFiles\Application\Functions\function\<FunctionName>
,以检查特定于功能的日志。
答案 1 :(得分:1)
据此: https://docs.microsoft.com/en-us/sandbox/functions-recipes/durable-diagnostics
您必须捕获异常并使用记录器将其记录下来,才能在应用洞察力中看到它:
catch (FunctionFailedException ex)
{
log.Error("Some Exception occured ", ex);
}