How to Easily Debug a Timer Trigger Azure Function Locally

Adam Murchison, 14 August 2020

When trying to run a timer triggered Azure Function locally it is unconventional to wait for the timer to hit or updating the timer to a small amount of time to debug. Instead you can add in a parameter to trigger the function so that it runs instantaneously.

Important notes:

You’ll need to have the Azure Function tools installed on your machine. For myself, I need to run Visual Studio 2017 in Administrator mode to allow debugging of Azure Functions for Microsoft Azure Storage Emulator.

Method Signature:

In the method signature, define your CRON expression to whatever you want your function to run as, documentation is here.

You’ll need to add in to your method signature:

#if DEBUG
     RunOnStartup= true
#endif

This simply means, if the function is running in debug mode, then the function will run instantaneously.

[FunctionName("Function1")]
public static void Run([TimerTrigger("0 30 9 * * *",
#if DEBUG
    RunOnStartup= true
#endif
    )]TimerInfo myTimer, TraceWriter log)
{
    log.Info("This is some information about your function.");
    log.Warning("This is a warning that occurred in your function.");
    log.Error("This is an error that occurred in your function");

    TraceEvent traceEvent = new TraceEvent(TraceLevel.Info, "An event that occurred in your code, now you want to trace");
    log.Trace(traceEvent);
}

Put in your break point, and you’ll see something like the below:

image

Summary:

You don’t have to waste time waiting for your Timer Trigger to hit a specific point in time, instead you can use this RunOnStartup parameter to trigger your timer functions instantly.