Reputation: 309
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
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.
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
Reputation:
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.json
file, add the Instrumentation Key value
Running the Function Locally:
In the Azure Portal:
View both File System Logs as well as Application Insights Logs by switching to the required option in Log Stream:
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.
Yes, You can check the previous (old) logs also using the TimeRange option provided in the portal as you see below:
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:
Upvotes: 7