Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function fails to bind ILogger

My function is referencing an assembly that references Microsoft.Extensions.Logging.Abstractions 2.0.0. If I add a nuget reference to that version to the function's assembly, function execution fails with:

[1/25/2018 11:14:46 PM] Microsoft.Azure.WebJobs.Host: Error indexing method 'TrainingFunction.Run'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'log' to type ILogger. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).

Is it possible to use the newer logger in Azure functions ? (SDK 1.0.7)

like image 640
RA. Avatar asked Jan 25 '18 23:01

RA.


People also ask

What are the disadvantages of using ILogger in Azure Functions?

Azure Functions natively support binding to a ILogger parameter on the static method of the function however there are currently some disadvantages. It does not support binding ILogger<T> or ILoggerFactory parameters, does not write entries to the local dev console and does not write to the Azure Portal log section.

How do I use ILogger as my logging interface?

To use ILogger as your logging interface, simply add a parameter to your function signature and use any of the logger extensions: public static void Run (string myQueueItem, ILogger logger) { logger.LogInformation ("C# Queue trigger function processed: {message}", myQueueItem); }

Is it possible to log Azure Functions in Visual Studio?

It is possible that this will change in the future. The default Azure Functions template in Visual Studio uses TraceWriter for logging. This works well when developing locally with Visual Studio and also writes log entries out to the Azure Portal log section for the function when deployed to Azure.

How do you bind parameters to an azure function?

Then a logger factory and module are registered (more on this later). The way this works is that Azure Functions will invoke the extension point in order to bind values to the marked parameters of the static entry point of the function. The binder will return the parameter value by resolving the target type from the Autofac container.


1 Answers

What is probably happening is that the SDK is binding to version X of the ILogger assembly and your user code is binding to version Y. The binding engine then doesn't recognize your parameter's type as being the same since they're from different assemblies. (this can happen with any other type too).

Generally the fix is to:

  1. See the nugget references used by the SDK
  2. Use those existing references and don't add the same dll with a different version.
like image 119
Mike S Avatar answered Sep 19 '22 01:09

Mike S