'Not able to debug Azure function V4 with Timer Trigger from Visual studio 2022

I'm trying to run/debug a new Azure function (V4, dotnet 6.0) from Visual Studio 2022. The function uses TimerTrigger and to debug it I tried using RunOnStartup = true in the TimerTriggerAttribute. I also tried a POST call to http://localhost:7071/admin/functions/<functionName> with an empty request body {} from the postman. But no luck. The debugger won't hit the function.

I've another function decorated with HttpTrigger, which runs successfully. But the one with TimerTrigger is not getting called.

Below is the code for the function and the related settings.

Function

namespace ImageAnalysisFunction
{
    public class ImageAnalysis
    {

        [FunctionName("ImageAnalysis")]
        public void Run([TimerTrigger("*/5 * * * * *", RunOnStartup = true)] TimerInfo myTimer)
        {
            Debug.WriteLine($"Timer triggered at: {DateTime.UtcNow}");
        }
    }

}

local.sttings.json

{
   "IsEncrypted": false,
   "Values": {
      "AzureWebJobsStorage": "UseDevelopmentStorage=true",
      "FUNCTIONS_WORKER_RUNTIME": "dotnet"
   }
}

Startup.cs

[assembly: FunctionsStartup(typeof(ImageAnalysisFunction.Startup))]
namespace ImageAnalysisFunction
{
    /// <summary>
    /// Startup class to configure dependency injection, swagger and other stuff
    /// </summary>
    public class Startup : FunctionsStartup
    {
        /// <inherit />
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddLogging();
        }

        /// <inherit />
        public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
        {
            base.ConfigureAppConfiguration(builder);
#if DEBUG
            FunctionsHostBuilderContext context = builder.GetContext();
            builder
              .ConfigurationBuilder
              .AddJsonFile(Path.Combine(context.ApplicationRootPath, $"appsettings.{context.EnvironmentName}.json"), optional: true, reloadOnChange: false);
#endif
        }
    }
}

Runtime output

enter image description here



Solution 1:[1]

Azure Functions is built on top of the App Service Infrastructure.

Debug.WriteLine method writes the information to the trace listeners in the Listeners collection about the debug.

As given in this SO Thread, System.Diagnostics.Trace class instead of Debug is the better approach given for adding the log messages to application logs.

Azure Function .NET 6 Timer Trigger - VS 2022 AzFuncNet6TimerTriggerDebugVS2022

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HariKrishnaRajoli-MT