Home > Software engineering >  What is the simplest way to set up logging for an azure function?
What is the simplest way to set up logging for an azure function?

Time:01-16

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:

  1. Created the Azure Function Queue Trigger through Visual Studio 2022.

  2. Add the Application Insights Package to the project through NuGet Package Manager i.e., enter image description here

  3. While Publishing the Project from Visual Studio, configure the Application Insights enter image description here

  4. 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. enter image description here

  5. In the local.settings.json file, add the Instrumentation Key value

enter image description here

  1. Publish the Project to the function app in Azure Portal and enable the App Insights present under Settings Menu.

enter image description here Running the Function Locally:

enter image description here

In the Azure Portal:

  1. 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.

enter image description here

  1. 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.

enter image description here

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

enter image description here

  1. You can also view the Log Stream under Monitoring Menu when running the functions like below: enter image description here

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

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

enter image description here

To Know more information about Live Metric Stream & Logs in Application Insights Resource, please refer this enter image description here

Also, the metrics available for both the previous and the current execution count of the function, request count, response count, server failures count, etc.

enter image description here

enter image description here

References:

  1. Application Insights Overview & Configuring
  2. Azure Functions Streaming Logs

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"
    }
  }
}
  •  Tags:  
  • Related