Created
January 17, 2024 18:23
-
-
Save swaters86/6f2eee47a28c1b7334466d28c11a81de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using System.Timers; | |
using log4net; | |
using log4net.Config; | |
class Program | |
{ | |
private static readonly ILog log = LogManager.GetLogger(typeof(Program)); | |
private static readonly HttpClient client = new HttpClient(); | |
private static Timer timer; | |
static void Main(string[] args) | |
{ | |
// Configure log4net | |
XmlConfigurator.Configure(); | |
// Set up a timer to trigger every 15 minutes | |
timer = new Timer(15 * 60 * 1000); | |
timer.Elapsed += OnTimedEvent; | |
timer.AutoReset = true; | |
timer.Enabled = true; | |
Console.WriteLine("Press Enter to exit."); | |
Console.ReadLine(); | |
} | |
private static void OnTimedEvent(Object source, ElapsedEventArgs e) | |
{ | |
CheckApiConnection().GetAwaiter().GetResult(); | |
} | |
private static async Task CheckApiConnection() | |
{ | |
try | |
{ | |
// Replace with your API URL | |
var response = await client.GetAsync("https://yourapi.com/api"); | |
if (!response.IsSuccessStatusCode) | |
{ | |
log.Error($"API check failed with status code: {response.StatusCode}"); | |
} | |
} | |
catch (HttpRequestException ex) | |
{ | |
// Log detailed connection failure information | |
log.Error("Failed to connect to the API", ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment