在Azure持久功能活动中使用ILogger?

Mar*_*tin 1 c# cloud azure azure-devops azure-functions

我正在使用天蓝色函数作为表插入事件触发器,特别是持久函数,并且我试图在我的活动函数中生成一些日志。

问题是活动函数没有收到任何“ILogger”,编排器也没有收到任何“ILogger”,因此我没有任何访问权限,也无法生成用于调试的日志。

整体流程为:

HTTP 请求 => Duarble HTTP 启动器 => 持久功能协调器 =>持久功能活动

有没有办法为从 ILogger 派生的某些类创建记录器实例?或者也许是一种将 ILogger 实例从 HTTP 启动器传递到活动函数的方法?

任何解决方案将不胜感激!

Har*_*ran 6

默认情况下,ILogger实例会注入到您的函数中,除非您使用DI.

您需要做的就是使用ILogger注入到您的函数中的 。例子:

[FunctionName("Demo")]
public async static Task RunOrchestrator(
    [OrchestrationTrigger] DurableOrchestrationContext context,
    ILogger log)
{
    log.LogInformation("Starting Orchestration function");
}
Run Code Online (Sandbox Code Playgroud)

如果您使用的话,Dependency injection您应该在启动时执行以下操作 builder.Services.AddLogging();

更多信息 :

https://github.com/Azure/azure-functions-host/issues/4858

https://github.com/Azure/azure-functions-host/issues/2720#issuecomment-503627355

微软文档