protected void SetState(WatchableServiceState newState, string message = null) { bool sendError = false; bool sendWarning = false; bool sendRestore = false; lock (StateChangeHistory) { if (State != newState) { StateChangeHistory.Add(new StateChangeItem { Time = DateTime.UtcNow, OldState = State, NewState = newState, Message = message }); if (newState == WatchableServiceState.Error) { sendError = _sendErrorEmail; } else if (newState == WatchableServiceState.Warning) { sendWarning = _sendWarningEmail; } else if (State != WatchableServiceState.Undefined) { sendRestore = _sendRestoreEmail; } State = newState; } } if (sendError) { _logger.LogError($"{message}"); if (_emailService != null) { _emailService.SendEmail(_adminEmails, _emailErrorSubject, $"Service '{DisplayName}' error: {message}"); } } else if (sendWarning) { _logger.LogWarning($"{message}"); if (_emailService != null) { _emailService.SendEmail(_adminEmails, _emailWarningSubject, $"Service '{DisplayName}' warning: {message}"); } } else if (sendRestore) { _logger.LogInformation($"Service '{Name}' restored"); if (_emailService != null) { _emailService.SendEmail(_adminEmails, _emailRestoreSubject, $"Service '{DisplayName}' restored"); } } }
protected void SetState(WatchableServiceState newState, Exception ex) { _logger.LogError(ex, ex.Message); SetState(newState, ex.InnerException?.Message ?? ex.Message); }