-1

Update

What is the difference between ILoggerFactory and ILogger<> with MS logging on Azure Functions 2, which supports Application Insights?

Code below:

    public class Functions
    {
        //other code
        private ILogger _log;

        public Functions(ILoggerFactory loggerFactory, ILogger<Functions> log)
        {
            _log = loggerFactory.CreateLogger<Functions>();
            _log = log;
        }

        [FunctionName("Token")]
        public async Task<IActionResult> Function1(
            [HttpTrigger()]...)
        {
               _log.LogInformation("Function1 invoked");
        }
    }

Update 2

ILogger: If ILogger is used as constructor parameter, the error below occurs. It works if it is used as function parameter.

What is the service lifetime for ILogger instance?

Is there a plan to support it on CTOR?

[04/06/2019 10:06:12] Executed 'SampleFunction' (Failed, Id=3912a5b7-10fa-41e1-9
0f8-fba9d2cda49b)
[04/06/2019 10:06:12] Microsoft.Extensions.DependencyInjection.Abstractions: Una
ble to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while att
empting to activate 'Microsoft.Azure.Functions.Samples.DependencyInjectionBasic.
SampleFunction'.
[04/06/2019 10:06:12] An unhandled host error has occurred.
[04/06/2019 10:06:12] Microsoft.Extensions.DependencyInjection.Abstractions: Una
ble to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while att
empting to activate 'Microsoft.Azure.Functions.Samples.DependencyInjectionBasic.
SampleFunction'.
Pingpong
  • 7,681
  • 21
  • 83
  • 209
  • PD of https://stackoverflow.com/questions/51345161/should-i-take-ilogger-iloggert-iloggerfactory-or-iloggerprovider-for-a-libra – HariHaran Jun 03 '19 at 06:16

1 Answers1

0

If you just want to use the default logging support, which logs to Application Insights, you just want to use an ILogger instance as a parameter on your function signature. Note that this is separate from ILogger<T> that you mentioned in your question. For an example, check out the official documentation.

If you want to use custom logging, the best approach is to implement a custom ILoggerProvider using the new Dependency Injection support for Azure Functions.

Connor McMahon
  • 1,318
  • 7
  • 15
  • Thanks, I update OP mentioning Application Insight. `ILogger` is not mentioned. The problem is that `ILogger` causes exception that it cannot be resolved by IoC. I am not sure if it is just me. – Pingpong Jun 03 '19 at 22:55
  • By `ILogger`, I was referring to `ILogger` which is in the code sample you posted. The correct type is just `ILogger`, not the parameterized type. The example code in the documentation I linked to shows this. If that gives you exceptions as well, then please post those exceptions in the question. – Connor McMahon Jun 03 '19 at 23:03
  • Why not `ILogger` and `ILoggerFactory`? There are FOUR different types. I thought `ILoggerFactory` will be more performant. Are both `ILogger` and `ILoggerProvider` the best for the scenarios you mentioned in terms of performance, threading, and IO write etc? – Pingpong Jun 04 '19 at 00:12
  • I'm not sure the exact reason why these types were chosen, but Functions is using dependency injection to ensure that it provides an ILogger implementation that logs to Application Insights. They do not provide implemenatations for `ILogger` or `ILoggerFactory`, likely due to the amount of development resources it would take to support implementations of those types as well. That way all development can ensure that the implementation is as optimal for as many scenarios as possible. For `ILoggerProvider`, you provide your own implementation, so it is only as performant as you write it. – Connor McMahon Jun 04 '19 at 00:29
  • I use this `ILogger` or `ILoggerFactory` with https://github.com/serilog/serilog-sinks-applicationinsights, and Application Insight works. Are they still good to use in terms of performance? – Pingpong Jun 04 '19 at 00:32
  • I can't speak to the performance of the implementations of those methods, as they were not written by the Azure Functions team. – Connor McMahon Jun 04 '19 at 19:05
  • As for your second update, I believe the ILogger instance is injected at function execution runtime, so it is following a scoped service lifetime of that function execution. Because of that, it does not make sense to support ILogger as a constructor parameter. – Connor McMahon Jun 04 '19 at 19:06