9

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
(...)
Rui
  • 175
  • 8
  • could you please share the `.csproj` code to see the dependencies, packages version installed and the namespaces using in function class like `using system` etc? –  Nov 27 '21 at 02:43
  • @HariKrishnaRajoli-MT added info in the question. – Rui Nov 29 '21 at 10:35
  • I have the exact same scenario and the exact same issue on my end as well... Have somebody found a solution for this? It's really weird since everything matches most of the documentation that can be found about this. – CesarD Jan 21 '22 at 17:52

3 Answers3

3

I've run into this, and found that if I keep the Microsoft.NET.Sdk.Functions dependency on 3.0.03, it works fine, even using .net6 and Azure Functions V4.

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

Ben
  • 2,962
  • 2
  • 20
  • 26
3

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 !

Alex Monkey
  • 1,427
  • 1
  • 14
  • 16
0

Similar to the anser of @Alex Monkey is to only skipping a particular DLL. Like so:

<ItemGroup>
    <FunctionsPreservedDependencies Include="Microsoft.Extensions.DependencyModel.dll" />
</ItemGroup>

Source reference here

LockTar
  • 5,364
  • 3
  • 46
  • 72