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

时间:2017-10-04 19:58:23

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

我已按照herehere所述的步骤在C#中的Visual Studio 2017中编写了预编译的Azure Function项目。我想通过Microsoft.Extensions.Logging.ILogger接口用Application Insights日志记录替换TraceWriter日志记录。为此,我将Microsoft.Azure.WebJobs Nuget软件包更新为版本2.1.0-beta1,其中包含ILogger绑定,可以直接绑定到Application Insights记录器。我还使用有效的Application Insights检测密钥添加了应用设置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的实例化仅适用于函数脚本?我希望情况并非如此,那么我应该在预先填充的函数设置中添加什么才能使其正常工作?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

我只是遇到这个问题,试图使用在Azure上运行的ip -o addr show | grep "ether" | awk -F': ' '{print $2}' 来获取.net Core 2.1功能应用。

我需要在“应用程序”设置中将Microsoft.NET.Sdk.Functions@1.0.14设置为FUNCTIONS_EXTENSION_VERSION。您可以在Azure门户中执行此操作。

有人提到https://stackoverflow.com/a/48461404/2896369