Azure Functions:配置 Slf4j 与 Log4j2 的绑定并将日志发送到 appinsights

Azure Functions: Configure Slf4j binding with Log4j2 and send logs to appinsights

我正在使用 Azure 函数,我需要使用 slf4j 与 log4j2 绑定来执行日志记录。我希望我的日志存储在 application insights (Azure monitor) 中。为此,我有一个用于配置的 log4j2.xml 文件,我在其中添加 A​​pplicationInsights Appender 并为应用程序洞察资源设置检测密钥。

目前的行为是,当我使用 ExecutionContext.getLogger.info("...") 时,我看到日志存储在应用洞察中,但这仅使用 java.util.logging . 但是,当我使用 slf4j 记录器进行记录时,日志不会存储在应用洞察中,我只能在控制台上看到它们。

使用 log4j2 绑定配置 slf4j 以将日志发送到 Azure Functions 中的 app-insights 的正确方法是什么?这是可能的,还是 azure 函数仅支持 java.util.logging 用于记录到 appinsights 的库?

Azure Functions 的默认行为是使用 java.util.logging 库将日志发送到 application insights。我没有在 application insights 中看到 slf4j 日志的原因是我没有从我的 pom.xml 中排除其他日志库。 添加所有排除后,slf4j-log4j2 组合工作正常,我可以在我的应用程序洞察资源中看到日志。