1

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:

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

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 ?

1 Answer 1

1

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);
 });
Sign up to request clarification or add additional context in comments.

3 Comments

It seems that this requires installing the Arcus Observability nuget package. Is there a way to do it without that?
@HEldursi Have a look here
We also provide templates for you to scaffold projects templates.arcus-azure.net

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.