azureloggingdependency-injectionazure-functions

Azure Function App Dependency Inject Logger not logging


I have a Function App that I’m trying to use DI in a class for the logger so that I don’t have to pass the ILogger to every function. I’ve been doing some research and found where I’m supposed to use ILogger in the class constructor. I’ve done all of the changes I believe are correct but it’s still not logging anything from that class.

I’ve added the logging to my Startup class, updated the hosts.json file, and used ILogger in the constructor. There are no errors but nothing gets logged. Does anyone see something I’m missing?

Startup.cs

public override void Configure(IFunctionsHostBuilder builder)
{
    builder.Services.AddLogging();
    builder.Services.AddTransient<IDataManager, DataManager>();
}

Host.json

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "default": "Debug"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": false,
        "excludedTypes": "Request"
      }
    }
  }
}

DataManager.cs

public class DataManager : IDataManager
{
    private DataContext _db;
    private readonly ILogger<DataManager> _log;

    public DataManager(ILogger<DataManager> log, DataContext dataContext)
    {
        _db = dataContext;
        _log = log;
    }
}

Solution

  • I have reproduced it in my environment and got the expected results as below and the code taken from @nareshnagpal06 's GitHub repository:

    Startup.cs:

    using Microsoft.Azure.Functions.Extensions.DependencyInjection; // install nuget - "Microsoft.Azure.Functions.Extensions"
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    [assembly: FunctionsStartup(typeof(FunctionApp50.Startup))]
    
    namespace FunctionApp50
    {
        public class Startup : FunctionsStartup
        {
            public override void Configure(IFunctionsHostBuilder builder)
            {
                builder.Services.AddSingleton<IHelperClass, HelperClass>();
    
            }
        }
    }
    

    HelperClass.cs:

    using Microsoft.Extensions.Logging;
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace FunctionApp50
    {
        public class HelperClass : IHelperClass
        {
            private static ILogger<IHelperClass> _logger;
            public HelperClass(ILogger<IHelperClass> logger)
            {
                _logger = logger;
            }
            public void Dowork()
            {
                _logger.LogInformation("Dowork: Execution Started");
                /* rest of the functionality below
                    .....
                    .....
                */
                _logger.LogInformation("Dowork: Execution Completed");
            }
        }
    }
    

    IHelperClass.cs:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace FunctionApp50
    {
        public interface IHelperClass
        {
            void Dowork();
        }
    }
    

    Function1.cs:

    using System;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Host;
    using Microsoft.Extensions.Logging;
    
    namespace FunctionApp50
    {
        public class Function1 // Ensure class is not static (which comes by default)
        {
            private IHelperClass _helper;
    
            public Function1(IHelperClass helper)
            {
                _helper = helper;
            }
    
            [FunctionName("Function1")]
            public void Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log)
            {
                log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
                // call helper 
                _helper.Dowork();
            }
        }
    }
    

    host.json:

    {
      "version": "2.0",
      "logging": {
        "fileLoggingMode": "always",
        "applicationInsights": {
          "samplingExcludedTypes": "Request",
          "samplingSettings": {
            "isEnabled": true
          }
        },
        "logLevel": {
          "Function": "Information"
        }
      }
    }
    

    Terminal Output:

    enter image description here

    File Output:

    Firstly go to run, then paste %temp%\LogFiles\Application\Functions.,then click on Host file in the location then click on log file then the output is:

    enter image description here