Azure 函数无法绑定 ILogger

Azure Function fails to bind ILogger

我的函数正在引用一个引用 Microsoft.Extensions.Logging.Abstractions 2.0.0 的程序集。如果我将对该版本的 nuget 引用添加到函数的程序集中,函数执行将失败并显示:

[1/25/2018 11:14:46 PM] Microsoft.Azure.WebJobs.Host: Error indexing method 'TrainingFunction.Run'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'log' to type ILogger. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).

是否可以在 Azure 函数中使用更新的记录器? (SDK 1.0.7)

可能发生的情况是,SDK 绑定到 ILogger 程序集的 X 版,而您的用户代码绑定到 Y 版。然后绑定引擎无法将您的参数类型识别为相同的,因为它们来自不同的程序集。 (这也可能发生在任何其他类型上)。

通常解决方法是:

  1. 查看 SDK 使用的 nugget 引用
  2. 使用那些现有的引用,不要添加不同版本的相同 dll。

对我来说,问题是我需要在 .csproj 文件中明确声明 Azure Functions 版本。

我在 <TargetFramework> 元素后添加了 <AzureFunctionsVersion>v2</AzureFunctionsVersion>

<PropertyGroup>
   <TargetFramework>netstandard2.0</TargetFramework>
   <AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>

希望对某人有所帮助:-)

绑定顺序也可能导致此失败。

正在更改参数顺序...

     [FunctionName("SomeFunction")]
     public static async Task Run([BlobTrigger("path", Connection = "conn")]
        ILogger logger, ExecutionContext context, Stream stream, string name) {}

...到...

     [FunctionName("SomeFunction")]
     public static async Task Run([BlobTrigger("path", Connection = "conn")]
        Stream stream, string name, ILogger logger, ExecutionContext context) {}

...解决了我的问题。 (Microsoft.NET.Sdk.Functions v1.0.24)

不知何故我也遇到了同样的错误,但这是 Microsoft.EntityFrameworkCore.SqlServer 导致问题的软件包版本。

将 Microsoft.EntityFrameworkCore.SqlServer v2.2.0 降级到 v2.1.4 就成功了。

我假设此软件包的 logging.abstractions 个库之间存在版本不匹配。

正如一位 MS 员工所述,原因可能是:

we don't quite yet support .NET Core 2.2, but we have the work underway and should ship in January.

https://github.com/Azure/azure-functions-host/issues/3854#issuecomment-449034719

同样错误的另一个原因...

不知何故,我最终得到了一个引用 Microsoft.Build.Framework 的 using 语句,它有自己的 ILogger 版本,修复只是将其替换为应用程序期望的 Microsoft.Extensions.Logging

对我来说,我在我的 Azure 函数引用的项目上使用了 NuGet 包 Microsoft.Extensions.Logging。卸载了这个包,我的错误(这个确切的错误)消失了。 我实际上并不需要 NuGet 包来在我的 Azure 函数中使用 ILogger。

 [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)

编辑:同时降级到 Microsoft.Extensions.Http 2.1.0 解决了另一个出现此错误的问题。如果您仍然需要扩展包(例如 IHttpClientFactory),可能会因为 SDK 版本与 3.1.1.

冲突而出现此日志记录错误

将 Azure Functions/Durable 函数从 v2 更新到 v3 时也会发生这种情况。您需要手动编辑 .csproj 以解决 ILogger 引用问题。

<PropertyGroup>
  <TargetFramework>netcoreapp3.1</TargetFramework>
  <AzureFunctionsVersion>v3</AzureFunctionsVersion>   
</PropertyGroup>
<ItemGroup>
    ...
   <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.2" />
</ItemGroup>

函数会继续报运行-time模块错误。然后你必须尝试在 Visual Studio 中创建新的 Azure Function v3 项目来下载 运行-time 模块的模板。官方文档中提供了有关该过程的更多信息:Azure Functions runtime versions overview

我不得不从解决方案中卸载 Microsoft.Extensions.Logging nuget 包。

自动更新我的 Nuget 包后,我突然开始收到 ILogger 绑定错误。

我在我的解决方案文件中找不到任何对 ILogger 扩展的明确引用。原来罪魁祸首是一个不同的扩展 Microsoft.Extensions.Caching.Memory,它更新到 v5.00(.NET 5?)。将该软件包降级到之前的版本 3.1.15 修复了错误(我最初是 v3.1.4)。我想知道是否有 v5.x 扩展包会触发此问题。

对我有用的是将“Microsoft.Extensions.Http”从版本 5.x.x.x 降级到“3.1.20”,这隐含地降级了“[=18=” ]”到“3.1.20”

似乎与 Http 库的版本 5.x 和“Microsoft.Extensions.Logging”版本 5.x 存在隐式依赖关系,并且该版本的日志记录存在一些问题。