Reputation: 48
I'm trying to use use Serilog.ILogger in my Azure function. I'm setting it up so I can add custom properties down the line. When I try to run the Azure function a console window pops up and shows the following error:
[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
This is the code that I'm using in my startup:
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);
}
I'm also injecting an instance of ILogger<> into a few classes in my function too.
Any thoughts of what might be going wrong here ?
Upvotes: 1
Views: 2528
Reputation: 222582
Can you try the following code?
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);
});
Upvotes: 1