protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _logger.LogDebug("Worker in: {time}", DateTimeOffset.Now.ToLocalTime()); foreach (string host in _serviceConfigurations.Hosts) { var exception = new Exception(); _logger.LogDebug($"Checking Host Availability: {host}"); var resultado = new ResultMonitor { Host = host, Hour = DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") }; try { using Ping p = new Ping(); resultado.Status = p.Send(host).Status.ToString(); } catch (Exception ex) { resultado.Status = "Exception"; resultado.Exception = ex; exception = ex; } string jsonResultado = JsonConvert.SerializeObject(resultado); if (resultado.Exception == null) { _logger.LogInformation(200, jsonResultado); } else { _logger.LogError(400, exception, jsonResultado); } } _logger.LogInformation("Worker finish in: {time}", DateTimeOffset.Now.ToLocalTime()); await Task.Delay(_serviceConfigurations.Interval, stoppingToken); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker executando as: {time}", DateTimeOffset.Now); foreach (var host in _serviceConfigurations.Hosts) { _logger.LogInformation($"Verificando a disponibilidade do host { host }"); var resultMonitor = new ResultMonitor() { Time = DateTime.Now.ToString("yyy-Mm-dd HH:mm:ss"), Host = host }; try { using (var p = new Ping()) { var response = p.Send(host); resultMonitor.Status = response.Status.ToString(); } } catch (Exception ex) { resultMonitor.Status = "Exception"; resultMonitor.Exception = ex; } var jsonResult = JsonConvert.SerializeObject(resultMonitor); if (resultMonitor.Exception == null) { _logger.LogInformation(jsonResult); } else { _logger.LogError(jsonResult); } } await Task.Delay(_serviceConfigurations.Interval, stoppingToken); } }