使用Azure Log Analytics在Azure Function v3中进行结构化日志记录

问题描述

我试图通过这样登录Azure函数来使用结构化日志记录:

log.LogError("Failed to get object. ({customerId},{objectId},{errorMessage})",request.CustomerId,request.ObjectId,errorMessage);

我的Azure功能通过诊断设置插入了Log Analytics工作区。

是否可以从消息中提取customerId,objectId和errorMessage?

例如,我想创建一个显示每个customerId错误数量的仪表板。

我尝试“提取字段”功能无济于事。我需要使用Application Insights和自定义维度吗?

解决方法

您几乎已经走对了路。在上面共享的示例日志语句示例中,如果启用了内置App Insights Integration,则应该已经在Application Insights的customDimentions日志中看到了customerId,objectId,errorMessage。这就是确切的原因,Loging in .net core是用这种方式进行模板制作的。您还可以在Azure Function structured logging中找到详细信息。 enter image description here

但是您提到您正在通过诊断设置使用Log Analytics工作区,但我认为该设置尚不支持结构化日志记录(当前处于预览状态,因此将来可能会更改)。 enter image description here

因此,Application Insights将是您的理想之选。无论如何,除了App Insights的内置查询窗格外,您都可以通过Log Analytics查询应用程序见解。