How to Monitor C# Cron Jobs with CronRabbit
C#The Problem
C# console applications and hosted services running as scheduled tasks can crash, throw unhandled exceptions, or fail to complete without notification.
The Solution
Use HttpClient to ping CronRabbit upon task completion. Integrate directly in your Main method or IHostedService.
Integration
Use .NET's HttpClient to send a GET request to CronRabbit. Register a shared HttpClient via dependency injection for proper connection pooling.
Code Examples
Console app with monitoring
C#using System.Net.Http;
class Program
{
private static readonly HttpClient client = new()
{
Timeout = TimeSpan.FromSeconds(10)
};
const string Ping = "https://ping.cronrabbit.com/your-id";
static async Task Main()
{
await client.GetAsync($"{Ping}/start");
try
{
await RunDataImport();
await client.GetAsync(Ping);
}
catch
{
await client.GetAsync($"{Ping}/fail");
throw;
}
}
}