Fred
Fred

Reputation: 309

What is the simplest way to set up logging for an azure function?

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"
    }
  }
}

Upvotes: 2

Views: 11580

Answers (2)

Alex
Alex

Reputation: 18526

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. Otherwise you will only see the logs in the live view, but they are not actually stored in app insights.

See also https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#logging-services

The host injects ILogger and ILoggerFactory services into constructors. However, by default these new logging filters are filtered out of the function logs. You need to modify the host.json file to opt-in to additional filters and categories.

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

Upvotes: 1

anon
anon

Reputation:

  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

My host.json:

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        }
    }
}

To Know more information about Live Metric Stream & Logs in Application Insights Resource, please refer this Microsoft Documentation.

To enable dependencies logs if you have dependencies in your functions project, please refer to this Host.Json configuration documentation which gives the data about complete logging data about performance counters logging, dependencies logging, timeout value defining, etc.

Updated Answer:

Yes, You can check the previous (old) logs also using the TimeRange option provided in the portal as you see below:

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

Upvotes: 7

Related Questions