简体   繁体   English

如何在 Azure Functions 中使用结构化日志记录

[英]how to use structured logging in Azure Functions

I am using the relatively new ILogger (vs. TraceWriter) option in Azure functions and trying to understand how logs are captured.我在 Azure 函数中使用了相对较新的 ILogger(与 TraceWriter)选项,并试图了解如何捕获日志。

Here's my function:这是我的功能:

    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
    {
        log.LogTrace("Function 1 {level}", "trace");
        log.LogWarning("Function 1 {level}", "warning");
        log.LogError("Function 1 {level}", "error");

        return req.CreateResponse(HttpStatusCode.OK, "Success!!!!");
    }

When I look at the server logs, the LogFiles directory has a hierarchy.当我查看服务器日志时,LogFiles 目录具有层次结构。

在此处输入图片说明

The yellow highlighted file includes my log statements:黄色突出显示的文件包括我的日志语句:

2017-08-19T13:58:31.814 Function started (Id=d40f2ca6-4cb6-4fbe-a05f-006ae3273562)
2017-08-19T13:58:33.045 Function 1 trace
2017-08-19T13:58:33.045 Function 1 warning
2017-08-19T13:58:33.045 Function 1 error
2017-08-19T13:58:33.075 Function completed (Success, Id=d40f2ca6-4cb6-4fbe-a05f-006ae3273562, Duration=1259ms)

The structured directory contains nothing here, but it seems to have various "codeddiagnostic" log statements in my real function applications directory.结构化目录在这里什么都不包含,但在我的实际功能应用程序目录中似乎有各种“编码诊断”日志语句。

What should I expect here?我应该在这里期待什么? Ultimately, I would like to have a single sink for logging from all of my application components and take advantage of structured logging across the board.最终,我希望有一个接收器来记录我所有应用程序组件的日志,并全面利用结构化日志记录。

The best way to collect structured logging from Azure Functions is to use Application Insights.从 Azure Functions 收集结构化日志记录的最佳方法是使用 Application Insights。 One you defined that your Logger is based on ILogger, you can define a Template that specifies the properties you want to log.如果您定义了您的 Logger 基于 ILogger,您可以定义一个模板来指定您要记录的属性。 Then on Application Insights traces, using the Application Insights Query Language (aka Kusto) you can access the values of each of these properties with the name customDimensions.prop__{name}.然后在 Application Insights 跟踪中,使用 Application Insights 查询语言(又名 Kusto),您可以使用名称 customDimensions.prop__{name} 访问每个属性的值。

You can find a sample of how to do this with Azure Functions v2 on this post https://platform.deloitte.com.au/articles/correlated-structured-logging-on-azure-functions您可以在这篇文章https://platform.deloitte.com.au/articles/correlated-structured-logging-on-azure-functions上找到有关如何使用 Azure Functions v2 执行此操作的示例

I had the same question.我有同样的问题。 The log file logger doesn't really respect structured logging, but if you use AppInsights for Azure Functions it will in fact add custom properties for your structured logging日志文件记录器并不真正尊重结构化日志记录,但如果您使用 AppInsights for Azure Functions,它实际上会为您的结构化日志记录添加自定义属性

I had a conversation going about it here https://github.com/Azure/azure-webjobs-sdk-script/issues/1675我在这里进行了一次对话https://github.com/Azure/azure-webjobs-sdk-script/issues/1675

Just FYI: Azure Functions running in isolated mode (.NET5 and .NET6) don't support structured logging using the ILogger from DI or provided in the FunctionContext.仅供参考:以隔离模式(.NET5 和 .NET6)运行的 Azure Functions 不支持使用来自 DI 的 ILogger 或在 FunctionContext 中提供的结构化日志记录。 As of November 2021, there is an open bug about it: https://github.com/Azure/azure-functions-dotnet-worker/issues/423截至 2021 年 11 月,有一个关于它的公开错误: https : //github.com/Azure/azure-functions-dotnet-worker/issues/423

As I understand it, the bug happens because all ILogger calls goes through the GRPC connection between the host and the isolated function, and in that process the message gets formatted instead of sending the original format and arguments.据我了解,发生错误是因为所有 ILogger 调用都通过主机和隔离函数之间的 GRPC 连接,并且在该过程中消息被格式化而不是发送原始格式和参数。 The Azure Insights connection that would record the structured log runs on the host and only receives the final message.将记录结构化日志的 Azure Insights 连接在主机上运行并且仅接收最终消息。

I plan on investigating some king of workaround, playing with direct access to Azure Insights inside the isolated process.我计划研究一些解决方法之王,在隔离的过程中直接访问 Azure Insights。 If it works, I'll post the workaround in a comment at the bug linked above.如果它有效,我将在上面链接的错误的评论中发布解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM