ILogger instance not initialized in parameter of precompiled Azure function

Viewed 656

I have written a precompiled Azure Function project in Visual Studio 2017 in C# following the steps described here or here. I want to replace TraceWriter logging with Application Insights logging through the Microsoft.Extensions.Logging.ILogger interface. For that I updated the Microsoft.Azure.WebJobs Nuget package to version 2.1.0-beta1, which includes the ILogger binding that enables binding directly to the Application Insights logger. I have also added the app setting APPINSIGHTS_INSTRUMENTATIONKEY with a valid Application Insights instrumentation key. After replacing the TraceWriter parameter of my function with an ILogger parameter, I trigger the function and get the following error response:

{
    "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)"
}

So no instance of ILogger can be created. However, if I create a project with a function script, instead of a precompiled function, then the ILogger instance is correctly instantiated. Does this mean that the instantiation of ILogger only works with function scripts?? I hope that's not the case, so what should I add to the setup of my precompled function to make it work?

2 Answers

As indicated in the answer pointed by Mikhail I had to download the Nuget package Microsoft.NET.Sdk.Functions v1.0.4. But to be able to install it I had to install version 2.1.0-beta4 of packages Microsoft.Azure.WebJobs and Microsoft.Azure.WebJobs.Extensions; and version 1.0.0-beta4 of Microsoft.Azure.Webjobs.Extensions.Http. And to get the project to build I had to add <Target Name="RunResolvePublishAssemblies" /> in the project file.

I just had this issue trying to get a .net Core 2.1 function app using Microsoft.NET.Sdk.Functions@1.0.14 running on Azure.

I needed to set FUNCTIONS_EXTENSION_VERSION to beta in the Application settings. You can do this in the Azure Portal.

This was mentioned https://stackoverflow.com/a/48461404/2896369.

Related