Search code examples
azure-functionserror-logging

Azure function log info not output


I have an azure function which does not appear to log error entries, but does log info entries.

This is my host.json:

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "default": "Information",
      "Host.Results": "Error",
      "Function": "Trace",
      "Host.Aggregator": "Trace"
    }
  }
}

And this is the code I'm using to log:

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using System;
using System.Data.SqlClient;
using System.Threading.Tasks;

namespace DatabaseCleanup
{
    public static class DataCleanup
    {
        [FunctionName("DataCleanup")]
        public static async Task Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, TraceWriter log)
        {
            try
            {
                log.Info($"Data cleanup started: {DateTime.Now}");
                // do work
                log.Info($"Data cleanup ended: {DateTime.Now}");
                throw new Exception("test error");
            }
            catch (Exception ex)
            {
                log.Error("Error thrown", ex); // this does not show up in logs
            }
        }
    }
}

And here is what the logs look like in Kudu:

2022-06-07T15:25:00.008 [Info] Function started (Id=1d293e12-9e66-4973-a304-aed31ea23f5a)
2022-06-07T15:25:00.008 [Info] Data cleanup started: 6/7/2022 3:25:00 PM
2022-06-07T15:25:02.448 [Info] Data cleanup ended: 6/7/2022 3:25:02 PM
2022-06-07T15:25:02.448 [Info] Function completed (Success, Id=1d293e12-9e66-4973-a304-aed31ea23f5a, Duration=2448ms)

Solution

  • We reproduced the same scenario and it's working fine.

    Please change time from minutes to seconds(suggestion) and generate an error with known case like 2 divides by 0, then its defiantly caught in catch block.

    Example code:- Try to generate the error by doing the 2 divides by 0 then it will move to the catch block like in the below sample code,

    using System;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Host;
    using Microsoft.Extensions.Logging;
    
    namespace TimeFunctionCheckErrorLog
    {
        public class Function1
        {
            [FunctionName("Function1")]
            public void Run([TimerTrigger("*/5 * * * * *")]TimerInfo myTimer, ILogger log)
            {
                try
                {
                    log.LogInformation($" Log Information executed at: {DateTime.Now}");
                    log.LogError("Test Error");
                    log.LogWarning("Log warning");
                    log.LogInformation("Log information");
                    int i = 0;
                    int reuslt = 2 / i;
                }
                catch (Exception ex)
                {
                    log.LogError($"Error : {ex.Message}");
                }
            }
        }
    }
    

    Output for the above code:

    enter image description here

    Kudu logs:

    enter image description here