In my azure function code I use ILogger which is supplied by dependency injection. The code looks like this:
public QueueTriggerFunction(IConfiguration configuration,
ILogger<QueueTriggerFunction> logger)
{
_config = configuration;
_logger = logger;
_logger.LogInformation("Creating QueueTriggerFunction");
}
[FunctionName("QueueTrigger")]
public async Task Run([QueueTrigger("%AzureStorage:Queue%")] AzureQueueModel task, IBinder binder)
{
_logger.LogInformation($"C# Queue trigger function processed: {task}");
When i publish this code and run it in Azure I was expecting to be able to see the logs somewhere but I can't figure out where. I've been trying to read up on this but I'm getting nowhere. I see that something called "Application Insights" is mentioned a lot. Do you have to use that? Ideally I would just like to see my log messages somehow.
I have host.json file that looks like this. I understand this has some importance for the logging:
{
"version": "2.0",
"logging": {
"fileLoggingMode": "always",
"logLevel": {
"default": "Information"
}
}
}
CodePudding user response:
Created the Azure Function Queue Trigger through Visual Studio 2022.
Add the Application Insights Package to the project through NuGet Package Manager i.e.,

While Publishing the Project from Visual Studio, configure the Application Insights

After Configuring (creating the App Insights Instance), you will get the App Insights Connection String > Get them copied into any text editor and Click Next > Finish.

In the
local.settings.jsonfile, add the Instrumentation Key value

- Publish the Project to the function app in Azure Portal and enable the App Insights present under Settings Menu.
Running the Function Locally:

In the Azure Portal:
- Open the function app storage account in one tab of the browser and your function in another tab. While running this function, create a queue and message in it to check the function is running well.

- You can see the logs that one insertion happened in the queue in the above console in test window. Also, you can see server requests, execution count, memory usage in the overview pane of your function app in the portal.

- You can see the metric logs like Response time, Requests in Queue, execution count, server failures, etc. under the Metrics in Monitoring Menu.

- You can also view the Log Stream under Monitoring Menu when running the functions like below:

View both File System Logs as well as Application Insights Logs by switching to the required option in Log Stream:

- You can also view the logs using KQL Queries.
- View the Live Metrics Data in the Application Insights Resource as you can see below:

To Know more information about Live Metric Stream & Logs in Application Insights Resource, please refer this 
Also, the metrics available for both the previous and the current execution count of the function, request count, response count, server failures count, etc.
References:
CodePudding user response:
I set up a full sample with working logging. The most critical part is that you either need to use the logger which gets injected into your trigger to log, or you need to explicitly enable the logger you get injected in the constructor. Loglevel default might not be enough in this case.
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
},
"logLevel": {
"Functions_V3_sample": "Information"
}
}
}


