c# - HTTP 触发 Azure 函数:OutOfMemoryException 未登录监视器
问题描述
我只是在浏览 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 中的一个已知问题吗?
解决方案
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>
以检查特定于功能的日志。
推荐阅读
- assembly - MASM X86 装配程序中发生无限循环
- javascript - 反应虚拟列表可更新项目计数
- libvlc - 无法在 VLC 播放器的两个实例中播放视频流
- c# - 子实体的 AutoMapper MapExpression 失败
- haskell - 在 aeson 的解析器中收集对象的所有未使用字段的更好方法?
- ruby-on-rails - 如何使用设计发送确认电子邮件?
- asciidoc - 在 PDF 中突出显示 TypeScript JSX (TSX) 的 AsciiDoc 语法
- react-native - FrescoModule 试图覆盖 com.facebook.react.modules.fresco.FrescoModule
- vuejs2 - Facebook Pixel 与 Vue 项目
- python - 根据索引将大数据帧中的数据除以较小数据帧中的数据