Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error loading Serilog from IConfiguration in Azure Function

I'm getting the following error when trying to setup Serilog from IConfiguration in an Azure Function.

Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

The error is thrown at runtime on the line:

var loggerConfiguration = new LoggerConfiguration().ReadFrom.Configuration(Configuration);

My Startup class looks like below:

public class Startup : FunctionsStartup
{
    public IConfiguration Configuration { get; set; }

    public override void Configure(IFunctionsHostBuilder builder)
    {
        Configuration = builder.GetContext().Configuration;

        (...)

        var loggerConfiguration = new LoggerConfiguration().ReadFrom.Configuration(Configuration);

        var logger = loggerConfiguration.CreateLogger();
        builder.Services.AddLogging(lb => lb.AddSerilog(logger));
    }
}

I can see the Microsoft.Extensions.DependencyModel(3.0.0) dependency in my project's dependencies, as a child dependency of Serilog.Settings.Configuration(3.3.0).

I can hard code the Serilog configuration, and have this working for example with a MSSqlServer sink, but the point is to have this configurable through a simple settings change.

I'm using .net6 and azure functions v4.

Edit: adding .csproj sample

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.1.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
    <PackageReference Include="Serilog.Sinks.MSSqlServer" Version="5.6.1" />
  </ItemGroup>
(...)

Usings in Startup.cs:

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Serilog;
using System.Reflection;
using Microsoft.Extensions.Logging;
(...)
Other project references and company libs
(...)
like image 533
Rui Avatar asked May 25 '26 18:05

Rui


1 Answers

Found an alternative solution in another thread.

Set _FunctionsSkipCleanOutput to true in the project.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <Nullable>enable</Nullable>
    <SatelliteResourceLanguages>en-US</SatelliteResourceLanguages>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
  </PropertyGroup>

With this, you can use the latest SDK

<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.0" />

Happy logging 🙂!

like image 110
Alex Monkey Avatar answered May 27 '26 06:05

Alex Monkey