Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chinhvo/85966e5a4148c61f456d0c7bba6ac975 to your computer and use it in GitHub Desktop.
Save chinhvo/85966e5a4148c61f456d0c7bba6ac975 to your computer and use it in GitHub Desktop.
private int _executionCount = 0;
public bool IsEnabled { get; set; }
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using PeriodicTimer timer = new PeriodicTimer(_period);
while (
!stoppingToken.IsCancellationRequested &&
await timer.WaitForNextTickAsync(stoppingToken))
{
try
{
if (IsEnabled)
{
_executionCount++;
_logger.LogInformation(
$"Executed PeriodicHostedService - Count: {_executionCount}");
}
else
{
_logger.LogInformation(
"Skipped PeriodicHostedService");
}
}
catch (Exception ex)
{
_logger.LogInformation(
$"Failed to execute PeriodicHostedService with exception message {ex.Message}. Good luck next round!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment