question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Injecting ILogger<T> in Azure Functions throws an Exception

See original GitHub issue

In an Azure Function, an instance of ILogger is injected in the functions themselves. But I would like to inject an instance of ILogger / ILogger<T> / ILoggerFactory in my components registered with SimpleInjector. Doing so results in the following exception:

System.Private.CoreLib: Exception while executing function: TestGenericLogger. SimpleInjector: The registered delegate for type ILogger<GenericSpeaker> threw an exception. Unable to resolve service for type ‘Microsoft.Azure.WebJobs.Script.IFileLoggingStatusManager’ while attempting to activate ‘Microsoft.Azure.WebJobs.Script.Diagnostics.HostFileLoggerProvider’. Microsoft.Extensions.DependencyInjection: Unable to resolve service for type ‘Microsoft.Azure.WebJobs.Script.IFileLoggingStatusManager’ while attempting to activate ‘Microsoft.Azure.WebJobs.Script.Diagnostics.HostFileLoggerProvider’.

The full example can be found on this repo: https://github.com/ahocquet/simple-injectore-af-logging

Here’s a sample of my code:

[FunctionName("TestGenericLogger")]
public static IActionResult TestGenericLogger(
	[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
	HttpRequest req,
	ILogger log)
{
	DIConfig.Container.GetInstance<GenericSpeaker>().SayHello();

	return new OkResult();
}

The registration process looks like this:

public static class DIConfig
{
	public static readonly Container Container = new Container();

	public static void Init(IServiceCollection serviceCollection)
	{
		serviceCollection.AddSimpleInjector(Container);

		Container.Register<Speaker>();
		Container.Register<GenericSpeaker>();
		Container.Register<FactorySpeaker>();

		serviceCollection.BuildServiceProvider()
						 .UseSimpleInjector(Container);
	}
}

The dependency injection using IServiceCollection works great for ILogger<T> and ILoggerFactory. Therefore, there must be a way to crosswire the ILoggerFactory / ILogger<T>.

I could apply a composition root pattern and use an entry point in all my functions that will set the ILogger instance within a LoggingWrapper service. That would work, but that would let me unsatisfied. 🤔

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10

github_iconTop GitHub Comments

2reactions
dotnetjunkiecommented, Sep 12, 2019

Simple Injector is not able to inject dependencies into Azure functions and I advise not using method injection at all. We have other Azure-related questions on this forum that discuss this as well. Take a look.

1reaction
dotnetjunkiecommented, Mar 12, 2021

I created a new Azure Functions Integration page. This demonstrates how to integrate Simple Injector with Azure Functions v3.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Weird exception when trying to use Dependency Injection ...
Given a File->New->Azure Functions v2 App, I'm trying to get a reference to either an ILoggerFactory or an ILogger<T> . I'm doing this...
Read more >
Dependency Injection and ILogger in Azure Functions
I've updated the module above so that it no longer supports dependency injection of ILogger<T> and ILogger instances are still created with the ......
Read more >
Guide for running C# Azure Functions in an isolated worker ...
Learn how to use a .NET isolated worker process to run your C# functions in Azure, which supports non-LTS versions of .NET and...
Read more >
How to do Azure Functions Logging with DI
The workaround involves changing your dependency from ILogger<T> to ILoggerFactory and calling it passing the user category obtained from ...
Read more >
How To Setup Dependency Injection With Azure Functions ⚡
Configure Logging​​ The Run method in our Azure Function, ProcessWeatherDataFunction has the ILogger getting injected by the runtime. This is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found