Search code examples
azuref#azure-functionsilogger

F# errors with ILogger in Azure Functions v2


I have a quite big project with Azure Function v2 written in F#. I create a library in C# where in the constructor I can pass a ILogger log. If only I add this library in the Azure Function project, every function returns an error.

Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'log' to type ILogger. Make sure the parameter Type is supported by the binding

enter image description here

In F# I tried to add

[<FunctionName("organization")>]
let public organization
    ( [<HttpTrigger(AuthorizationLevel.Function, "post", Route = "organisations/{organisationId}")>]
        request : HttpRequestMessage,
        organisationId : string,
        executionContext : ExecutionContext,
        log : ILogger) =

      let PaymentGateway = PaymentGateway(Environment.GetEnvironmentVariable "Api_Secret_Key", log)

      ...

I saw some posts for example on GitHub or another post where other people have the same issue. Apparently, the solution is to update the Azure Function from v2 to v3 but I can't do that in my case right now.

Is there any solution?

Update

The .fsproj is

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Update="FSharp.Core" Version="4.6.2" />
  </ItemGroup>
</Project>

Solution

  • After a solid week of tests and changes, I found a non-ideal solution but it is working.

    In the function I define logInfo like that:

    let logInfo : Action<string> = new Action<string>(fun (x: string) -> log.LogInformation(x))
    

    In my C# library, I removed any reference to Microsoft.Extensions.Logging and I replace it with:

    private Action<string> _logger;
    public PaymentGateway(string apiKey, Action<string> logger) {
        _logger = logger;
    }
    

    At least, I have logs in my component. I don't like this solution but it is what I can do with F#.