gpt4 book ai didi

c# - 在Azure函数中使用Serilog.ILogger

转载 作者:行者123 更新时间:2023-12-03 02:26:48 29 4
gpt4 key购买 nike

我正在尝试在我的 Azure 函数中使用 Serilog.ILogger。我正在设置它,以便我可以添加自定义属性。当我尝试运行 Azure 函数时,会弹出控制台窗口并显示以下错误:

Error Message

[2021-03-16T00:15:25.424Z] A host error has occurred during startup operation '6a853374-f99b-4ca4-8bde-8df8cc1e7e79'.[2021-03-16T00:15:25.426Z] func: Invalid host services. Microsoft.Azure.WebJobs.Script.WebHost: The following service registrations did not match the expected services:[2021-03-16T00:15:25.427Z] [Missing] ServiceType: Microsoft.Extensions.Logging.ILoggerProvider, Lifetime: Singleton, ImplementationType: Microsoft.Azure.WebJobs.Script.Diagnostics.FunctionFileLoggerProvider, Microsoft.Azure.WebJobs.Script, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null

这是我在启动时使用的代码:

 public override void Configure(IFunctionsHostBuilder builder)
{
var serilogLogger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.ApplicationInsights(TelemetryConverter.Traces)
.CreateLogger();
builder
.Services
.AddHttpClient()
.AddLogging(l =>
{
l.ClearProviders();
l.AddSerilog(serilogLogger);
})
.AddSingleton(serilogLogger);
}

我还将 ILogger<> 的实例注入(inject)到我的函数中的几个类中。

有什么想法可能会出现问题吗?

最佳答案

你能试试下面的代码吗?

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Configuration;
using Serilog.Events;


var logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.Enrich.WithComponentName(ComponentName)
.Enrich.WithVersion()
.WriteTo.Console()
.WriteTo.AzureApplicationInsights(instrumentationKey)
.CreateLogger();


builder.Services.AddLogging(l =>
{
l.ClearProvidersExceptFunctionProviders();
l.AddSerilog(logger);
});

关于c# - 在Azure函数中使用Serilog.ILogger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66647525/

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