Azure-Functions icon indicating copy to clipboard operation
Azure-Functions copied to clipboard

Injected ILogger using Autofac doesn't log anything on AzureFunctionTools window (AzureFunctionsV2)

Open joaoantunes opened this issue 7 years ago • 3 comments

I'm using ILogger abstraction of Microsoft.Extensions.Logging in other to do some logging.

If I do the logging in the Run of the Azure Function it logs to the AzureFunctionTools window without any issues.

But I need logs also in other classes of the application, not only on azure functions, and I want to use the same system!

I'm using Autofac as DI so after reading a while I did this:

public class AutoFacServiceProviderBuilder : IServiceProviderBuilder
{
	private readonly IConfiguration configuration;

	public AutoFacServiceProviderBuilder(IConfiguration configuration) 
		=> this.configuration = configuration;

	public IServiceProvider Build()
	{
		var services = new ServiceCollection();
		services.AddTransient<ITransientService, TransientService>();
		services.AddScoped<IScopedService, ScopedService>();

		var builder = new ContainerBuilder();

		// Registered the ILoggerFactory to provide the logger
        // on the classes where is logging a need 
		builder.Register(s => services.AddLogging().BuildServiceProvider()
			.GetService<ILoggerFactory>()).As<ILoggerFactory>();

		builder.RegisterType<SingletonService>().As<ISingletonService>().SingleInstance(); 

		builder.Populate(services);
		return new AutofacServiceProvider(builder.Build());
	}
}

Then here I've used the logging on a class different than AzureFunction:

public class TransientService : ITransientService
{
	private readonly ILogger logger;
	private int counter = 0;

	public TransientService(ILoggerFactory loggerFactory)
	{
		// I've used this logger name because I've read this (written in 27/May/2018):
		// here: https://www.neovolve.com/2018/04/05/dependency-injection-and-ilogger-in-azure-functions/
		// Azure Functions(v2 beta) applies a filter out of the box for ILogger.The filter looks
		// at whether the logger name is either Function.Something or Function.Something.User.Any 
		// logger that does not have its name matching this format will have its log messages filtered out.
		string loggerName = "Function." + typeof(LazyExample).FullName + ".User";
		this.logger = loggerFactory.CreateLogger(loggerName);
		this.logger.LogError($"I'm creating the class: {nameof(LazyExample)}");
	}

	public int GetCounter()
	{
		this.logger.LogInformation($"Counting {this.counter} on {nameof(TransientService)} class.");
		return this.counter++;
	}
}

In the following image you can see the logs that were made on the function or the internal loggins of azure functions, but my loggings doesn't show:

Result when I run the function

How can I reuse Logger implementation provided by Microsoft on my own classes and injected by Autofac?

joaoantunes avatar Sep 06 '19 09:09 joaoantunes

You need to modify your host.json. See accepted answer here: https://stackoverflow.com/questions/54945769/why-is-the-ilogger-injected-in-my-azure-function-v2-missing-the-appinsights-inst

tynor88 avatar Sep 07 '19 09:09 tynor88

I've changed the project to use this host.json:

  "version": "2.0",
  "logging": {
    "fileLoggingMode": "debugOnly",
    "logLevel": {
      "default": "Trace"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "maxTelemetryItemsPerSecond": 5
      }
    }
  }

But the result was the same, the logger created by the Factory doesn't log on the azure function tool window.

In fact those configurations made a change on a simple prototype, where I was using only .NET DI and I was using ILogger instead of ILoggerFactory loggerFactory. The problem is if I try to use a class using ILogger the autofac is not able to resolve that dependency.

joaoantunes avatar Sep 10 '19 15:09 joaoantunes

Also having this problem. guidance would be appreciated.

jsangco avatar Dec 02 '19 02:12 jsangco