1

I wrote an Azure function that is triggered every day at 4AM. I published it and it had its first successful run today at 4AM. I am sure that it ran successfully, because it put the expected data in the database.

[FunctionName("MyFunction")]
public async Task RunAsync([TimerTrigger("0 0 4 * * *")]TimerInfo myTimer, ILogger log)
{
    // function body
}

I am using the log object to log information and error logs in the function body:

log.LogInformation("This is information log.");
// ...
log.LogError("This is error log.");

If I connect to the Log stream in Azure while the function runs, I see the logs. Where can I find them later? Only now I set the diagnostic setting to send FunctionAppLogs to Log Analytics workspace:

Send function app logs to Analytics workspace

Will it resolve my issue?

Where can I see the details about executions (success/failure/time and maybe resource consumption) of my function? If I go to Function app, then choose functions, choose MyFunction from the list and go to the "Monitor" blade, I see only two failures (out of many!) from a few days ago. This failures were logged when the function was triggered via HTTP GET (now it's triggered by timer).

Only two failures visible

EDIT

This is the content of my hosts.json file:

{
    "version": "2.0",
    "logging": {
        "fileLoggingMode": "always",
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true
            }
        },
        "logLevel": {
            "default": "Information",
            "Host.Results": "Error",
            "Function": "Error"
        }
    }
}

My understanding is that Application Insights should show some data (samplingSettings is enabled) and I should have the logs for my function (default is Information). I am unsure about the Function: Error setting only. In the documentation it is explained in the following way:

For logs of Host.Results or Function, only log events at Error or a higher level.
...
For all other logs, including user logs, log only Information level and higher events.

Are the logs that I create in code "user logs" or "logs for Function"?

Michal B.
  • 5,676
  • 6
  • 42
  • 70

1 Answers1

0

Where can i find them later?

enter image description here

Where can I see the details about executions?

Azure Functions creates an internal Application insight to monitor them, you can use a host.json file to configure log levels, sampling and so on.

host.json sample:

{
    "version": "2.0",
    "aggregator": {
        "batchSize": 1000,
        "flushTimeout": "00:00:30"
    },
    "extensions": {
        "blobs": {},
        "cosmosDb": {},
        "durableTask": {},
        "eventHubs": {},
        "http": {},
        "queues": {},
        "sendGrid": {},
        "serviceBus": {}
    },
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0)"
    },
    "functions": [ "QueueProcessor", "GitHubWebHook" ],
    "functionTimeout": "00:05:00",
    "healthMonitor": {
        "enabled": true,
        "healthCheckInterval": "00:00:10",
        "healthCheckWindow": "00:02:00",
        "healthCheckThreshold": 6,
        "counterThreshold": 0.80
    },
    "logging": {
        "fileLoggingMode": "debugOnly",
        "logLevel": {
          "Function.MyFunction": "Trace",
          "default": "None"
        },
        "applicationInsights": {
            "samplingSettings": {
              "isEnabled": true,
              "maxTelemetryItemsPerSecond" : 20,
              "evaluationInterval": "01:00:00",
              "initialSamplingPercentage": 100.0, 
              "samplingPercentageIncreaseTimeout" : "00:00:01",
              "samplingPercentageDecreaseTimeout" : "00:00:01",
              "minSamplingPercentage": 0.1,
              "maxSamplingPercentage": 100.0,
              "movingAverageRatio": 1.0,
              "excludedTypes" : "Dependency;Event",
              "includedTypes" : "PageView;Trace"
            },
            "enableLiveMetrics": true,
            "enableDependencyTracking": true,
            "enablePerformanceCountersCollection": true,            
            "httpAutoCollectionOptions": {
                "enableHttpTriggerExtendedInfoCollection": true,
                "enableW3CDistributedTracing": true,
                "enableResponseHeaderInjection": true
            },
            "snapshotConfiguration": {
                "agentEndpoint": null,
                "captureSnapshotMemoryWeight": 0.5,
                "failedRequestLimit": 3,
                "handleUntrackedExceptions": true,
                "isEnabled": true,
                "isEnabledInDeveloperMode": false,
                "isEnabledWhenProfiling": true,
                "isExceptionSnappointsEnabled": false,
                "isLowPrioritySnapshotUploader": true,
                "maximumCollectionPlanSize": 50,
                "maximumSnapshotsRequired": 3,
                "problemCounterResetInterval": "24:00:00",
                "provideAnonymousTelemetry": true,
                "reconnectInterval": "00:15:00",
                "shadowCopyFolder": null,
                "shareUploaderProcess": true,
                "snapshotInLowPriorityThread": true,
                "snapshotsPerDayLimit": 30,
                "snapshotsPerTenMinutesLimit": 1,
                "tempFolder": null,
                "thresholdForSnapshotting": 1,
                "uploaderProxy": null
            }
        }
    },
    "managedDependency": {
        "enabled": true
    },
    "retry": {
      "strategy": "fixedDelay",
      "maxRetryCount": 5,
      "delayInterval": "00:00:05"
    },
    "singleton": {
      "lockPeriod": "00:00:15",
      "listenerLockPeriod": "00:01:00",
      "listenerLockRecoveryPollingInterval": "00:01:00",
      "lockAcquisitionTimeout": "00:01:00",
      "lockAcquisitionPollingInterval": "00:00:03"
    },
    "watchDirectories": [ "Shared", "Test" ],
    "watchFiles": [ "myFile.txt" ]
}

The aggregator indicates log flush time and limit number of executions to be logged. Azure functions use Adaptive Sampling for AI and the volume is adjusted automatically to keep within a specified maximum rate of traffic, and is controlled via the setting MaxTelemetryItemsPerSecond

To configure/disable Sampling in Azure functions for development environments you can manipulate host.json like this:

{
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": false,
        "maxTelemetryItemsPerSecond" : 20,
        "excludedTypes": "Request;Exception"
      }
    }
  }
}

Change your loglevel to "trace" in your host.json.

Trace logs:

enter image description here

EDIT

enter image description here

public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }

Host.json:

{
    "version": "2.0",
   "logging": {
        "fileLoggingMode": "debugOnly",
        "logLevel": {
          "Function.MyFunction": "Trace",
          "default": "None"
        },
        "applicationInsights": {
            "samplingSettings": {
              "isEnabled": false,
              "maxTelemetryItemsPerSecond" : 20,
              "evaluationInterval": "01:00:00",
              "initialSamplingPercentage": 100.0, 
              "samplingPercentageIncreaseTimeout" : "00:00:01",
              "samplingPercentageDecreaseTimeout" : "00:00:01",
              "minSamplingPercentage": 0.1,
              "maxSamplingPercentage": 100.0,
              "movingAverageRatio": 1.0,
              "includedTypes" : "PageView;Trace"
            },
            "enableLiveMetrics": true,
            "enableDependencyTracking": true,
            "enablePerformanceCountersCollection": true,            
            "httpAutoCollectionOptions": {
                "enableHttpTriggerExtendedInfoCollection": true,
                "enableW3CDistributedTracing": true,
                "enableResponseHeaderInjection": true
            },
            "snapshotConfiguration": {
                "agentEndpoint": null,
                "captureSnapshotMemoryWeight": 0.5,
                "failedRequestLimit": 3,
                "handleUntrackedExceptions": true,
                "isEnabled": true,
                "isEnabledInDeveloperMode": false,
                "isEnabledWhenProfiling": true,
                "isExceptionSnappointsEnabled": false,
                "isLowPrioritySnapshotUploader": true,
                "maximumCollectionPlanSize": 50,
                "maximumSnapshotsRequired": 3,
                "problemCounterResetInterval": "24:00:00",
                "provideAnonymousTelemetry": true,
                "reconnectInterval": "00:15:00",
                "shadowCopyFolder": null,
                "shareUploaderProcess": true,
                "snapshotInLowPriorityThread": true,
                "snapshotsPerDayLimit": 30,
                "snapshotsPerTenMinutesLimit": 1,
                "tempFolder": null,
                "thresholdForSnapshotting": 1,
                "uploaderProxy": null
            }
        }
    }
}

ExcludedTypes node has been removed.

UPDATE: Function -> Functions -> click in your function -> Monitor -> configure AI

enter image description here

enter image description here

Juanma Feliu
  • 1,298
  • 4
  • 16
  • I editted my question and added my hosts.json file. I have a question regarding my configuration. Could you have a look and let me know if you know the answer? – Michal B. Mar 24 '21 at 08:55
  • If you are not in a production environment use: "samplingSettings": { "isEnabled": false } so you are logging everything. You can only log last 90 days in AI. – Juanma Feliu Mar 24 '21 at 09:19
  • Thanks for the advice. However, I would like to log things in production and this is my problem currently. Are the logs that I create in code "user logs" or "logs for Function"? I am trying to figure out why I am missing logs for my function in Azure. – Michal B. Mar 24 '21 at 09:22
  • Sampling works like that. If you have 500 requests within 10 minutes with a 200 response and a 90% Sampling you will get a simplified view of your requests/response logged. https://learn.microsoft.com/en-us/azure/azure-monitor/app/sampling – Juanma Feliu Mar 24 '21 at 09:28
  • Your logs should be displayed in Application Insights as "Trace". Adding a picture in the comment above. – Juanma Feliu Mar 24 '21 at 16:12
  • Disable Sampling please and downgrade your log level to "Trace" – Juanma Feliu Mar 24 '21 at 17:22
  • What's the easiest way to see whether the function executed successfully or failed with an exception? If I publish a function with a bug and it fails I do not see any information about it. I only know it failed, because I miss the expected data in the database... :/ – Michal B. Mar 25 '21 at 15:38
  • Use try catch block and log error inside your function. Log.LogError("X") You should be able to check everything in application insights -> Search button. – Juanma Feliu Mar 25 '21 at 15:54
  • I see the logs after directing them to Log Analytics workspace. Can you advise me whether I should create separate Log Analytics workspaces for separate functions or rather direct logs from all functions to a single workspace? – Michal B. Mar 29 '21 at 14:00
  • updated: how i did to configure your function with a new or existing Application Insights. – Juanma Feliu Mar 29 '21 at 14:35