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

前端 未结 5 1859
后悔当初
后悔当初 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:43

    I managed to resolve this problem:

    Injecting into my class as below:

    MyClass.cs:

    public class MyClass
    {
        private readonly ILogger _logger;
    
        public MyClass(ILogger logger)
        {
            _logger = logger;
        }
    }
    

    Startup.cs:

    [assembly: FunctionsStartup(typeof(Namespace.Startup))]   
    
    namespace Namespace {    
    public class Startup : FunctionsStartup 
    {
        public override void Configure(IFunctionsHostBuilder builder) 
        {
            builder.Services.AddLogging(); 
        }
      }
    }
    

提交回复
热议问题