Logging information that are not included in the Azure function run method C#?

Abdallah Shukor 1 Reputation point
2021-12-11T12:51:12.24+00:00

I had a C# console application which was getting data from a database querying it and then awaits 1 hour to query new data. The program.cs was used as a startup class to do the dependency injections. The results shown on the console when running the console application are the following:

156851-consoleapplicationresults.png

I had to transform this console application to an azure timer trigger function so that it runs every 1hour, do the same job as explained above and log the same information as shown in the image. So what I did is that I copied the code from the program.cs file and pasted it in the timer trigger run method. Here is the code:

namespace ExportServiceFunctionApp  
{  
    public class ExportServiceFunctionApp  
    {  
        [FunctionName("ExportServiceFunctionApp")]  
        public static void Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log)  
        {  
            log?.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");  
  
            try  
            {  
                IServiceCollection services = new ServiceCollection();  
  
  
                services.AddSingleton<IDataReceiver, DataReceiver.DataReceiver>()  
                        .AddSingleton<IDataProcessor, DataProcessor.DataProcessor>()  
                        .AddSingleton<IDataClient, KustoDataClient>()  
                        .AddSingleton<IAzureBlobClient, AzureBlobClient>()  
                        .AddSingleton<IAzureKustoClient, AzureKustoClient>()  
                        .AddSingleton<IQueriesSettings, QueriesSettings>()  
                        .AddSingleton<IExportServiceSettings, ExportServiceSettings>()  
                        .AddTransient<IRawData, RawData>()  
                        .AddLogging();  
  
                IServiceProvider serviceProvider = services.BuildServiceProvider();  
  
                log?.LogInformation("Start initialization.");  
                var dataProcessor = serviceProvider.GetRequiredService<IDataProcessor>();  
  
                log?.LogInformation("Start data processing.");  
                dataProcessor.Start();  
            }  
            catch (Exception ex)  
            {  
            }  
        }  
    }  
}  

You can also find the structure of the FunctionApp here:

156852-functionapp.png

The ExportServiceFunctionApp.cs is executing successfully but what I am missing here is the logs coming from other classes. An example is the DataReceiver.cs (Note: There are other classes too). What I need is to have the same results and logs shown above in the console application.

The results of the function app are shown here:

156853-exportfunctionappresults.png

**As you can see here, the logs that are only showing on the console are the ones who are used in the Run method of the ExportServiceFunctionApp.cs but all other logs that are used in the other classes are not.

As an example: The ILogger is injected in the DataReceiver constructor but the logs are not appearing on the console when the FunctionApp is running locally. Here is the code of DataReceiver.cs**

public class DataReceiver : IDataReceiver  
    {  
        private readonly IExportServiceSettings _exportServiceSettings;  
        private readonly ILogger _logger;  
        private CancellationToken _cancellationToken;  
        private TimeSpan _interval;  
        private DateTime _lastTo;  
  
        public DataReceiver(IExportServiceSettings exportServiceSettings, ILoggerFactory loggerFactory)  
        {  
            _logger = loggerFactory?.CreateLogger<DataReceiver>();  
            _exportServiceSettings = exportServiceSettings;  
        }  

The host.json has the following structure:

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

Can someone please tell me how to log the messages coming from the other classes in the same way how they are shown in the results of the console application. I still don't know why only the logs in RUN method are being shown. I previously asked a similar question but I guess it wasn't explained in a good way.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,297 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,275 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pramod Valavala 20,591 Reputation points Microsoft Employee
    2021-12-14T07:27:50.747+00:00

    @Abdallah Shukor You seem to be building up a separate service collection instead of registering services the runtime's existing collection. With this, you can also register logger services into your other classes as well.

    Do ensure you update host.json with your logger so that they don't get filtered out.