One Developer
One Developer

Reputation: 566

Azure Function - ILogger is not logging?

I have an azure function, it logs the information without any issues.

namespace aspnetcore_azurefun_blob
{
    [StorageAccount("AzureWebJobsStorage")]
    public class FileTrigger
    {
        #region Property
        private readonly IFileProcessor fileProcessor;
        #endregion

        #region Constructor
        public FileTrigger(IFileProcessor fileProcessor)
        {
            this.fileProcessor = fileProcessor;
        }
        #endregion

        [FunctionName("FileTrigger")]
        public void ProcessFilesFromSamplesContainer([BlobTrigger("samples-workitems/{name}")]Stream myBlob, string name, ILogger log, ExecutionContext context)
        {
            log.LogInformation("Function: ProcessFilesFromSamplesContainer is called");
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");

            var result = fileProcessor.ProcessAsync(myBlob, name);

            log.LogInformation($"Function Completed Successfully on {DateTime.Now.ToLongDateString()} @ {DateTime.Now.ToShortTimeString()}.\n.");
        }

enter image description here

However, I also have the business logic implemented using DI and below is the excerpt of the implementation.

ServiceBase.cs

namespace BusinessService.Services.Common
{
    public abstract class ServiceBase<T>
    {
        public static AppDbContext AppDbContext;
        public static ILogger<T> Logger { get; set; }
        public static AppConfigurator Configurator { get; set; }

        public ServiceBase(AppDbContext appDbContext, ILogger<T> logger, IOptions<AppConfigurator> configurator)
        {
            AppDbContext = appDbContext ?? throw new ArgumentNullException(nameof(appDbContext));
            Logger = logger ?? throw new ArgumentNullException(nameof(logger));
            Configurator = configurator.Value ?? throw new ArgumentNullException(nameof(configurator));
        }
    }
}

FileProcessingService.cs

namespace BusinessService.Services
{
    public interface IFileProcessingService
    {
        void Process(Stream myBlob, string name);
    }

    public class FileProcessingService : ServiceBase<FileProcessingService>,  IFileProcessingService
    {
        #region Constructor
        public FileProcessingService(AppDbContext appDbContext, ILogger<FileProcessingService> logger, IOptions<AppConfigurator> configurator) 
            : base(appDbContext, logger, configurator) { }
        #endregion

        #region Public Methods
        public void Process(Stream myBlob, string name)
        {
            AppDbContext.FileRecords.Add(new FileRecords
            {
                FileName = name,
                IsCompleted = DefaultValues.IsCompleted
            });
            AppDbContext.SaveChanges();

            Logger.LogInformation("Reading configuration from the configuration settings file: {Configurator.AzureSQLServerConfigurator.ConnnectionString}");
            Logger.LogInformation("Database is updated..!");
        }
        #endregion

    }
}

Line#34 and #35 doesn't Log anything

            Logger.LogInformation("Reading configuration from the configuration settings file: {Configurator.AzureSQLServerConfigurator.ConnnectionString}");
            Logger.LogInformation("Database is updated..!");

DependencyRegistrar.cs

namespace CrossCutting.DependencyInjection
{
    public static class DependencyRegistrar
    {
        public static void Intialize(this IServiceCollection services)
        {
            // Initialize App Settings from Configurator Settings Json file
            services.AddOptions<AppConfigurator>()
                .Configure<IConfiguration>((settings, configuration) =>
                {
                    configuration.GetSection("AppConfigurator").Bind(settings);
                })
                .Validate((c) =>
                {
                    return !new[] { c.AzureSQLServerConfigurator.ConnnectionString }.Any(s => String.IsNullOrWhiteSpace(s));
                });
        }
    }
}

What am I missing so that FileProcessingService.cs will log the information ?

Upvotes: 1

Views: 1686

Answers (1)

Delliganesh Sevanesan
Delliganesh Sevanesan

Reputation: 4776

I have checked your code in our end I could get the logging information. In your code i have noticed in your code you are using the Logger instead of logger.

Because in your Region Constructor you are using ILogger<FileProcessingService> logger from here you have to call the logger to push your logging information into Application Insights/Function execution panel (Output console window)


 #region Constructor
        public FileProcessingService(AppDbContext appDbContext, ILogger<FileProcessingService> logger, IOptions<AppConfigurator> configurator) 
            : base(appDbContext, logger, configurator) { }
        #endregion


 #region Public Methods
        public void Process(Stream myBlob, string name)
        {
            AppDbContext.FileRecords.Add(new FileRecords
            {
                FileName = name,
                IsCompleted = DefaultValues.IsCompleted
            });
            AppDbContext.SaveChanges();

# changed Logger into logger          
logger.LogInformation("Reading configuration from the configuration settings file: {Configurator.AzureSQLServerConfigurator.ConnnectionString}");
            
logger.LogInformation("Database is updated..!");
        }
        #endregion

Still if not able to push the logs you can add your namespace in your host.json file to avoid missing the logging

{
  "version": "2.0",
  "logging": {
    "logLevel": {
        // Here you can use your Project namespace like BusinessService.Services
      "<namespace>": "Information" 
    }
  }
}

Upvotes: 2

Related Questions