ILogger 实例未在预编译 Azure 函数的参数中初始化
ILogger instance not initialized in parameter of precompiled Azure function
我在 Visual Studio 2017 年用 C# 编写了一个预编译的 Azure Function 项目,遵循 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 绑定描述的步骤,可以直接绑定到 Application Insights 记录器。我还使用有效的 Application Insights 检测密钥添加了应用程序设置 APPINSIGHTS_INSTRUMENTATIONKEY
。用 ILogger
参数替换函数的 TraceWriter
参数后,我触发了函数并得到以下错误响应:
{
"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。但是为了能够安装它,我必须安装软件包 Microsoft.Azure.WebJobs
和 Microsoft.Azure.WebJobs.Extensions
的版本 2.1.0-beta4;和 Microsoft.Azure.Webjobs.Extensions.Http
的 1.0.0-beta4 版。为了构建项目,我必须在项目文件中添加 <Target Name="RunResolvePublishAssemblies" />
。
我刚刚在尝试在 Azure 上使用 Microsoft.NET.Sdk.Functions@1.0.14
运行 获取 .net Core 2.1 函数应用时遇到了这个问题。
我需要在应用程序设置中将 FUNCTIONS_EXTENSION_VERSION
设置为 beta
。您可以在 Azure 门户中执行此操作。
有人提到了这个 。
我在 Visual Studio 2017 年用 C# 编写了一个预编译的 Azure Function 项目,遵循 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 绑定描述的步骤,可以直接绑定到 Application Insights 记录器。我还使用有效的 Application Insights 检测密钥添加了应用程序设置 APPINSIGHTS_INSTRUMENTATIONKEY
。用 ILogger
参数替换函数的 TraceWriter
参数后,我触发了函数并得到以下错误响应:
{
"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。但是为了能够安装它,我必须安装软件包 Microsoft.Azure.WebJobs
和 Microsoft.Azure.WebJobs.Extensions
的版本 2.1.0-beta4;和 Microsoft.Azure.Webjobs.Extensions.Http
的 1.0.0-beta4 版。为了构建项目,我必须在项目文件中添加 <Target Name="RunResolvePublishAssemblies" />
。
我刚刚在尝试在 Azure 上使用 Microsoft.NET.Sdk.Functions@1.0.14
运行 获取 .net Core 2.1 函数应用时遇到了这个问题。
我需要在应用程序设置中将 FUNCTIONS_EXTENSION_VERSION
设置为 beta
。您可以在 Azure 门户中执行此操作。
有人提到了这个