Azure Functions V4配置Serilog遇程序集加载异常,求解决方案
问题详情
本地运行Azure Functions V4时配置Serilog,已安装对应NuGet包,在startup.cs中完成Serilog配置,函数应用使用Microsoft.Extensions.Logging库的ILogger。移除lb.ClearProviders()无效,项目编译正常,但运行时func.exe抛出System.IO.FileNotFoundException,提示找不到Microsoft.Extensions.Logging, Version=7.0.0.0程序集,删除AppData\Local\AzureFunctionsTools文件夹也无法解决。
已安装的NuGet包
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" /> <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.2.0" /> <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.11.0" /> <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" /> <PackageReference Include="Serilog.AspNetCore" Version="7.0.0" /> <PackageReference Include="Serilog.Enrichers.CorrelationId" Version="3.0.1" /> <PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" /> <PackageReference Include="Serilog.Sinks.AzureTableStorage" Version="8.5.41" /> <PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
startup.cs 代码
Using 部分
using Microsoft.Azure.Functions.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Serilog; using Serilog.Events;
配置代码
Log.Logger = new LoggerConfiguration() .MinimumLevel.Warning() .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("Worker", LogEventLevel.Warning) .MinimumLevel.Override("Host", LogEventLevel.Warning) .MinimumLevel.Override("System", LogEventLevel.Error) .MinimumLevel.Override("Function", LogEventLevel.Error) .MinimumLevel.Override("Azure.Storage.Blobs", LogEventLevel.Error) .MinimumLevel.Override("Azure.Core", LogEventLevel.Error) .WriteTo.Console() .WriteTo.Async(a => a.AzureTableStorage( connectionString: "connection string goes here", storageTableName: "table name goes here", propertyColumns: new[] { "Application" }), bufferSize: 20, blockWhenFull: true) .Enrich.WithCorrelationId() .Enrich.WithCorrelationIdHeader("X-Correlation-ID") .Enrich.WithProperty("Application", "app name goes here") .CreateLogger(); builder.Services.AddLogging(lb => { lb.ClearProviders(); lb.AddSerilog(Log.Logger, true); });
报错信息
Exception thrown: 'System.IO.FileNotFoundException' in Microsoft.Azure.WebJobs.Host.dll
An exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.Azure.WebJobs.Host.dll but was not handled in user code
Could not load file or assembly 'Microsoft.Extensions.Logging, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
Could not load file or assembly 'Microsoft.Extensions.Logging, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
解决建议
- 降级Serilog.AspNetCore版本:当前使用的Serilog.AspNetCore 7.0.0依赖.NET 7,而Microsoft.NET.Sdk.Functions 4.2.0适配.NET 6,对应依赖的
Microsoft.Extensions.Logging是6.x版本。将Serilog.AspNetCore降级到6.1.0,该版本适配.NET 6,可避免版本冲突。 - 显式指定Microsoft.Extensions.Logging版本:在NuGet包中添加
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />,强制使用6.0.0版本,覆盖Serilog引入的7.0.0依赖。 - 检查Azure Functions Core Tools版本:确保本地安装的Core Tools是适配.NET 6的v4.x版本,若版本过新默认使用.NET 7,可尝试降级到对应.NET 6的Core Tools版本。
- 添加程序集绑定重定向:在项目的
app.config或web.config中添加以下配置,将7.0.0.0版本的程序集重定向到已安装的6.x版本:<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.Extensions.Logging" publicKeyToken="adb9793829ddae60" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> - 清理重建项目:执行
dotnet clean和dotnet build命令,确保所有依赖重新安装并正确引用。
内容的提问来源于stack exchange,提问作者206mph

