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

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

    UPDATE

    Reference Use dependency injection in .NET Azure Functions

    Registering services

    To register services, you can create a configure method and add components to an IFunctionsHostBuilder instance. The Azure Functions host creates an IFunctionsHostBuilder and passes it directly into your configured method.

    To register your configure method, you must add an assembly attribute that specifies the type for your configure method using the FunctionsStartup attribute.

    So in this case

    [assembly: FunctionsStartup(typeof(MyNamespace.Startup))]    
    namespace MyNamespace {
        public class Startup : FunctionsStartup {
            public override void Configure(IFunctionsHostBuilder builder) {
                //  ** Registers the ILogger instance **
                builder.Services.AddLogging();
    
                //  Registers the application settings' class.
                //...
    
                //...omitted for brevity    
            }
        }
    }
    

    ORIGINAL

    I believe since you have access to the service collection, you should be able to add logging to it

    public void Configure(IWebJobsBuilder webJobsBuilder) {       
    
        //  ** Registers the ILogger instance **
        webJobsBuilder.Services.AddLogging();
    
        //OR
        //webJobsBuilder.Services.AddLogging(builder => {
        //    //...
        //});
    
        //  Registers the application settings' class.
        //...
    
        //...removed for brevity
    }
    

    and having anILoggerFactory in the Function's constructor.

    //...
    
    //Ctor
    public FindAccountFunction(ILoggerFactory loggerFactory, IMapper mapper, IAccountWorkflow accountWorkflow) {
        m_logger = loggerFactory.CreateLogger<FindAccountFunction>();
        m_mapper = mapper;
        m_accountWorkflow = accountWorkflow;
    }
    
    //...
    
    0 讨论(0)
  • 2021-02-04 03:30

    You can add

      "logging": {
        "fileLoggingMode": "debugOnly",
        "logLevel": {
          "default": "Information"
        }
      }
    

    without the need to add DI if you want to use IloggerFactory

    0 讨论(0)
  • 2021-02-04 03:38

    In addition to registering the class in function Startup class, we would need to add the namespace in host.json file as well in order to log the messages to App insights. If we don't add it, it will simply log the message to console locally but when deployed on azure, it won't log anything and won't give any error either.

    {
      "version": "2.0",
      "logging": {
        "logLevel": {
          "FunctionProjectNameSpace.RegisteredClass": "Information"
        }
      }
    }
    

    Sample code here for reference - https://gist.github.com/nareshnagpal06/82c6b4df2a987087425c32adb58312c2

    0 讨论(0)
  • 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<AppSettings>();
    
            builder.Services.AddTransient<IMyService, MyService>();
        }
    }
    
    public MyFunction(IMyService service, ILogger<IMyService> logger)
    {
        this.service = service;
        this.logger = logger;
    }
    

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

    0 讨论(0)
  • 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<MyClass> _logger;
    
        public MyClass(ILogger<MyClass> 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(); 
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题