gpt4 book ai didi

azure - .Net6 Azure Functions 中的 ILogger 依赖项注入(inject)问题

转载 作者:行者123 更新时间:2023-12-03 03:34:11 27 4
gpt4 key购买 nike

我最近将 .NET 3.1 azure 函数代码转换为 .NET6。启动类中的以下代码工作正常,在作为 Azure 函数部署到 Azure 时,但在转换为 .NET6 后,我们收到以下错误。

Microsoft.Azure.WebJobs.Script.WebHost: Invalid host services detected. Microsoft.Azure.WebJobs.Script.WebHost: The following service registrations did not match the expected services: [Missing] ServiceType: Microsoft.Extensions.Logging.ILoggerProvider, Lifetime: Singleton, ImplementationType: Microsoft.Azure.WebJobs.Script.Diagnostics.FunctionFileLoggerProvider, Microsoft.Azure.WebJobs.Script, Version=4.11.0.0, Culture=neutral, PublicKeyToken=null [Missing] ServiceType: Microsoft.Extensions.Logging.ILoggerProvider, Lifetime: Singleton, ImplementationType: Microsoft.Azure.WebJobs.Script.Diagnostics.HostFileLoggerProvider, Microsoft.Azure.WebJobs.Script, Version=4.11.0.0, Culture=neutral, PublicKeyToken=null.

启动类代码

public class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.Services.AddCashMatchingService();
builder.Services.AddDataManagerService();

builder.Services.AddOptions();
builder.Services.AddSingleton<ILoggerFactory, LoggerFactory>();
builder.Services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
builder.Services.AddSingleton<ILoggerProvider>(p=>new DebugLoggerProvider());
builder.Services.AddLogging(lb => lb.ClearProviders()
.AddSerilog(
Activator.CreateInstance<LoggerConfiguration>().Enrich
.FromLogContext()
.WriteTo.AzureBlobStorage(Environment.GetEnvironmentVariable(
"azureStorageConnection-StorageConnection"),
(Serilog.Events.LogEventLevel)Enum.Parse(
typeof(Serilog.Events.LogEventLevel),
Environment.GetEnvironmentVariable("Serilog_MinimumLevel")),
Environment.GetEnvironmentVariable("Serilog_ContainerName"),
Environment.GetEnvironmentVariable("Serilog_StorageFileName"))
.WriteTo.ApplicationInsights(TelemetryConfiguration.CreateDefault(),
TelemetryConverter.Events)
.CreateLogger()));
}
}

最佳答案

不要清除 comment by Loul G 中提到的日志记录提供程序.

我也遇到了同样的问题。看起来 AZ Functions 由于某些未知原因需要这 2 个特定的记录器提供程序

关于azure - .Net6 Azure Functions 中的 ILogger 依赖项注入(inject)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73894647/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com