azure - ILogger 实例未在预编译 Azure 函数的参数中初始化

标签 azure azure-functions azure-webjobs azure-webjobssdk

我按照描述的步骤 here 在 Visual Studio 2017 中用 C# 编写了一个预编译的 Azure Function 项目或here 。我想通过 Microsoft.Extensions.Logging.ILogger 接口(interface)将 TraceWriter 日志记录替换为 Application Insights 日志记录。为此,我将 Microsoft.Azure.WebJobs Nuget 包更新到版本 2.1.0-beta1 ,其中包括 ILogger绑定(bind),允许直接绑定(bind)到 Application Insights 记录器。我还添加了带有有效 Application Insights 检测 key 的应用设置 APPINSIGHTS_INSTRUMENTATIONKEY。将函数的 TraceWriter 参数替换为 ILogger 参数后,我触发该函数并收到以下错误响应:

{
    "id": "497256d0-1c3a-4a10-a1cc-41b771305338",
    "requestId": "e546219e-7e38-4fa7-80cf-b06e0b8f4018",
    "statusCode": 500,
    "errorCode": 0,
    "messsage": "Exception while executing function: Functions.HelloHttpTrigger -> Exception binding parameter 'log' -> No value was provided for parameter 'log'.",
    "errorDetails": "Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: Functions.HelloHttpTrigger ---> System.InvalidOperationException : Exception binding parameter 'log' ---> System.InvalidOperationException : No value was provided for parameter 'log'.\r\n   at Microsoft.Azure.WebJobs.Host.Bindings.Invoke.ClassInvokeBinding`1.BindAsync(BindingContext context)\r\n   at async Microsoft.Azure.WebJobs.Host.Triggers.TriggeredFunctionBinding`1.BindCoreAsync[TTriggerValue](ValueBindingContext context,Object value,IDictionary`2 parameters) \r\n   End of inner exception\r\n   at Microsoft.Azure.WebJobs.Host.Executors.DelayedException.Throw()\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstance instance,IReadOnlyDictionary`2 parameters,TraceWriter traceWriter,CancellationTokenSource functionCancellationTokenSource)\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??) \r\n   End of inner exception\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)\r\n   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryExecuteAsync(IFunctionInstance functionInstance,CancellationToken cancellationToken)\r\n   at Microsoft.Azure.WebJobs.Host.Executors.ExceptionDispatchInfoDelayedException.Throw()\r\n   at async Microsoft.Azure.WebJobs.JobHost.CallAsyncCore(MethodInfo method,IDictionary`2 arguments,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.ScriptHost.CallAsync(String method,Dictionary`2 arguments,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.WebScriptHostManager.HandleRequestAsync(FunctionDescriptor function,HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.ExecuteAsync(HttpControllerContext controllerContext,CancellationToken cancellationToken)\r\n   at async System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.Handlers.SystemTraceHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async Microsoft.Azure.WebJobs.Script.WebHost.Handlers.WebScriptHostHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)\r\n   at async System.Web.Http.HttpServer.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)"
}

因此无法创建 ILogger 实例。但是,如果我使用函数脚本而不是预编译函数创建项目,则 ILogger 实例将被正确实例化。这是否意味着 ILogger 的实例化仅适用于函数脚本?我希望情况并非如此,那么我应该在预完成函数的设置中添加什么才能使其正常工作?

最佳答案

正如 Mikhail 指出的答案所示,我必须下载 Nuget 包 Microsoft.NET.Sdk.Functions v1.0.4。但为了能够安装它,我必须安装版本 2.1.0-beta4 的软件包 Microsoft.Azure.WebJobsMicrosoft.Azure.WebJobs.Extensions ;以及 Microsoft.Azure.Webjobs.Extensions.Http 的 1.0.0-beta4 版本。为了构建项目,我必须添加 <Target Name="RunResolvePublishAssemblies" />在项目文件中。

关于azure - ILogger 实例未在预编译 Azure 函数的参数中初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46573187/

相关文章:

Azure AD 将角色分配给组

Azure 网站标准定价自动缩放

azure - 如何查看azure服务计划占用的磁盘空间是什么?

python - Azure Functions Python 错误 - 没有作业函数

azure - 批量上传时触发一次 Azure Function

Azure WebJob,如果停止如何通知?

azure - 重新部署 Azure 虚拟机

spring-boot - 在 Azure Functions : Permission denied with server. 端口上运行 Spring Boot native 应用程序:80

Web Job 上的 Python 库

azure - Azure WebJobs 中的物理路径