2

I have an Azure Function that doesn't run in Azure. Locally it runs, but in Azure it doesn't. I have a try/catch in place, which doesn't catch any exception, but in the logs I get "2020-07-26T12:23:00.021 [Error] An exception occured." Don't understand what I'm doing wrong.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FluentEmail.Core;
using FluentEmail.Core.Models;
using FluentEmail.Mailgun;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace SendEmailFunction
{
    public static class SendEmailFunction
    {
        private const string EmailSubject = "Collaboration proposal";

        [FunctionName("SendEmailFunction")]
        public static async Task Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            try
            {
              var path = Path.Combine(Directory.GetCurrentDirectory(), "1.csv");
              List<string> lines = (await File.ReadAllLinesAsync(path)).ToList();
            }
            catch (Exception ex)
            {
                log.LogError($"An exception occured.", ex);
            }
        }
    }
}

This are the logs that I get:

2020-07-26T12:35:00.007 [Information] C# Timer trigger function executed at: 7/26/2020 12:35:00 PM

2020-07-26T12:35:00.030 [Error] An exception occured.

2020-07-26T12:35:00.045 [Information] Executed 'SendEmailFunction' (Succeeded, Id=8b5d1b47-37de-4f84-8936-d31b19f0f73d, Duration=42ms)

sam
  • 37
  • 6
  • 1
    "[Error] An exception occured." - not a very useful log message. How about logging what the contents of the exception were? – Neil Jul 26 '20 at 12:42
  • 1
    I'm going to guess that either your email settings are different on the server, or the email server isn't accepting requests from your Azure function. – Neil Jul 26 '20 at 12:45
  • 1
    I have the cod in a try/catch and I log the exception as can be seen in the code I've shared: catch (Exception ex) { log.LogError($"An exception occured.", ex); } but I don't get anything. My guess is that something causes the function to crush and the code is not getting to the catch part. – sam Jul 26 '20 at 12:47
  • 2
    you're using the wrong `LogError` overload for logging an exception: https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loggerextensions.logerror You're using `string, object[]` overload, rather than `exception, string` overload. While this won't actually correct your issue, it will give you more information about the exception. Additionally, log something meaningful in the "string" message, like the `ex.Message`. – Kritner Jul 26 '20 at 13:12
  • Thanks Kritner. Now I get the exception indeed. It appears not to find my files from which I try to read values. Changed the line to: log.LogError(ex, ex.Message); – sam Jul 26 '20 at 14:45
  • 2
    Don't even pass ex.Message... That gets automatically output as part of the formatting when you pass an exception as the first argument. Instead, put a meaningful message to YOU "Unable to pefrom Some Function" to help you trace it down later. Also look up usage of the ILogger interface. You should NOT be using string.Format or string interpolation `$""` you should use placeholders in your messages and then pass values for those placeholders as the additional parameters. It's all based on support for structured logging (which AppInsights and Azure logging support) – pinkfloydx33 Jul 26 '20 at 17:00

1 Answers1

1

Kritner's answer helped because it permited me to see the exception.

It was because it didn't find the file from which I read the values. I fixed that by constructing the path like this: var path = Path.Combine(context.FunctionAppDirectory, "1.csv");

Where context is ExecutionContex from Microsoft.Azure.WebJobs namespace.

It's fixed now.

Thanks!

sam
  • 37
  • 6