0

I am using TimerTrigger CRON job to schedule a task for 2 days, but the trigger doesn't seem to work. Following is the code which I tried,

public static void StartupJob([TimerTrigger("0 * * * * *", RunOnStartup = true)] TimerInfo timerInfo) //0 * * * * * added CRON job to run for every minute for testing purpose
        {
            Console.WriteLine("Timer job fired!");
        }

local.settings.json

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

As the above code wasn't working I have added the following to the main()

static void Main()
        {
            var config = new JobHostConfiguration();
            config.UseTimers();
            config.Tracing.ConsoleLevel = System.Diagnostics.TraceLevel.Verbose;

            if (config.IsDevelopment)
            {
                config.UseDevelopmentSettings();
            }

            var host = new JobHost(config);
            // The following code ensures that the WebJob will be running continuously
            host.RunAndBlock();
        }

Getting the following error and showing fix as to install DocumentFormat package, but installing this package isn't resolving the FunctionName error.

enter image description here

I'm a newbie to C# and unable to find what is the issue here. Any help or reference to good examples would be helpful and appreciated.

Thank you.

Shinchan
  • 81
  • 2
  • 17
  • I think perhaps you have one star too many in the CRON expression. I am not familiar with CRON, but one expression tester out there hints at this. – Bent Tranberg Nov 14 '21 at 18:55
  • Studying further, it looks like you should have "* * * * *" for every minute. – Bent Tranberg Nov 14 '21 at 18:58
  • @BentTranberg no this isn't working it's not triggering – Shinchan Nov 15 '21 at 02:56
  • Based on your posted code, it seems that you are missing `FunctionName` attribute. So attached it like `[FunctionName("StartupJob")]` – user1672994 Nov 15 '21 at 09:01
  • @user1672994 do I need to add any package to use FunctionName? – Shinchan Nov 15 '21 at 11:19
  • As such there is not need to add a new package. If you resolve the attribute in VS, you can get the namepsace which need to be added as part of using – user1672994 Nov 15 '21 at 11:36
  • @user1672994 when I add it VS is showing quick fix as to add a new class named FunctionName which I think isn't the correct fix – Shinchan Nov 15 '21 at 13:15
  • @Shinchan - it should not prompting to generate a new class. FunctionName attibute is part of functions sdk. Do you have the functions sdk refererenced as package reference? You can use [this](https://github.com/amigup/CleanArchitecture-For-AzureFunctionV3) as a reference. – user1672994 Nov 16 '21 at 04:34

1 Answers1

0

I tried to reproduce your issue and figured it out.

  1. Firstly, I have created Azure Function in Visual Studio and Selected the Timer Trigger > Click on create enter image description here

  2. As per your requirement to run this timer trigger function in every minute, I changed the expression to [TimerTrigger("0 * * * * *")]

enter image description here

  1. After that, build the project and run the function locally. The Output is:

enter image description here

  1. Created the Function App in Azure (consumption plan).
  2. Published the above function app to Azure function app from visual studio.
  3. Then, Go to Azure Portal > Your Function app > Functions (in the left index pane) > Click on your function name (in my case it is Function1) enter image description here

After that, Go to Code+Integration, Click on Test/Run your function and the logs will start logging the execution of function like below:

enter image description here

Example 2 (With the given code in the question):

enter image description here

enter image description here

The problem is that Console.Writeline is a static function. You can redirect its output (stdout) to somewhere else, but you can't trace where that statement was run from after it reached stdout.

In order for us to do our per Function invocation logging, we need to know which function it came from and which particular invocation id it belong to. This is what the TraceWriter does. From there, our logging engine pipes it out to Storage for our dashboard to pick up. We do pipe the stdout pipe to the WebJob logs, but it's not simple to tie it back to a given invocation.

For more information, follow this : https://github.com/Azure/azure-webjobs-sdk/issues/682

Other way one can use is to custom logging framework (e.g. Serilog) and direct log output to the Console.

For more information about logging samples, you can follow the below link :

https://learn.microsoft.com/en-us/sandbox/functions-recipes/logging?tabs=csharp