How can I use the new DI to inject an ILogger into an Azure Function using IWebJobsStartup?

前端 未结 5 1796
后悔当初
后悔当初 2021-02-04 03:18

I am using Azure Function v2. Here is my function that uses the constructor injection:

public sealed class FindAccountFunction
{
    private readonl         


        
5条回答
  •  逝去的感伤
    2021-02-04 03:42

    You should remove the call of AddLogging method from your startup class. The default logger is already setup by the azure function host.

    [assembly: WebJobsStartup(typeof(StartUp))]
    public class StartUp : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            builder.Services.AddSingleton();
    
            builder.Services.AddTransient();
        }
    }
    
    public MyFunction(IMyService service, ILogger logger)
    {
        this.service = service;
        this.logger = logger;
    }
    

    Instance methods are now supported with azure function since Azure Functions Runtime 2.0.12265

提交回复
热议问题