How to use Microsoft.Extensions.Logging.Abstraction 5.0.0 with Azure Durable Functions

2.4k Views Asked by At

I am working with Azure Functions and I am having a problem that involves the NuGet "Microsoft.Extensions.Logging.Abstractions". My solution has been running perfectly fine, but recently I added a dependency which depends on "Microsoft.Extensions.Logging.Abstractions 5.0.0". The project builds fine, but when I run it. the following error appears: "Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=5.0.0.0".

I found some information related to this on a Github issue. The suggestions that I found were:

  1. Downgrade "Microsoft.Extensions.Logging.Abstractions" to version 3.1.0. I can't do this, since I have a third-party dependency that references that same NuGet, but with version 5.0.0.
  2. Upgrade to .NET 5.0. I can't do this, since I need to use Durable Functions, and those are not yet supported in Azure Functions with .NET 5.0.

I also tried using multiple versions of that same NuGet, but I got the same error (I tried the approach that is mentioned in this link).

Is there any way of solving this problem?

UPDATE: I created a minimal example in which the issue can be reproduced. It is located here. The branch feature/WithLogging3 runs as expected, but the feature/WithLogging5 fails with the message "'The host has not yet started". If I enable the option to Break the Debugger with Common Language Runtime Exceptions, I get the error "Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified".

It seems that the project only fails when there is an ILogger being injected into a function. For instance, this one (taken directly from Visual Studio 2019 Template for Azure Functions with .NET Core 3.1):

[FunctionName("Function1")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        string name = req.Query["name"];

        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;

        string responseMessage = string.IsNullOrEmpty(name)
            ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
            : $"Hello, {name}. This HTTP triggered function executed successfully.";

        return new OkObjectResult(responseMessage);
    }

If I remove the injected ILogger (and the line in which the logger is used in the method), then the project runs fine. However, I need to use a logger.

I know that the example I provided does not include a Durable Function, but it reproduces the issue, and it will reproduce even if you add a Durable Function to the project, or if you replace the current function with a Durable Function that takes an injected ILogger.

By the way, if it changes anything, I am using Visual Studio 2019 (Enterprise).

1

There are 1 best solutions below

4
On BEST ANSWER

I tried to reproduce the issue as I got no errors for both .Net Core 3.1 and 6 Versioned Azure Durable Functions Project when installed the package Microsoft.Extensions.Logging.Abstractions version 5.0.0 through following steps.

  1. Created two projects of Azure Durable Functions in .Net Core 3.1 and 6.
  2. Added the package Microsoft.Extensions.Logging.Abstractions version 5.0.0 using dotnet core CLI command in the terminal of VS Code project.

dotnet add package Microsoft.Extensions.Logging.Abstractions --version 5.0.0

And Executed the functions one by one, as the following output shows it is running successfully:

Azure Durable Functions .Net Core 3.1 enter image description here

Azure Durable Functions .Net Core 6 enter image description here

Updated Answer:

Microsoft.Extensions.Logging.Abstractions version 5.0.0 is added to the minimal example provided to the Azure Function and it is running successfully. enter image description here

enter image description here