public WatchableService(string name, string displayName, ILoggerFactory loggerFactory, IEmailSender emailService, IConfiguration configuration) { Name = name; DisplayName = displayName; Metrics["Total Requests"] = TotalRequestCount.ToString(); Metrics["Successful Requests"] = SuccessfulRequestCount.ToString(); Metrics["Failed Requests"] = FailedRequestCount.ToString(); Metrics["Last Request Time"] = ""; Metrics["Min Request Time"] = ""; Metrics["Max Request Time"] = ""; Metrics["Avg Request Time"] = ""; Metrics["Total Request Time"] = ""; _logger = loggerFactory.CreateLogger(Name); _emailService = emailService; _adminEmails = configuration["Watcher:AdminEmails"]; _emailErrorSubject = configuration["Watcher:ErrorEmailSubject"] ?? "GRAFT Service Error (_service_name_)"; _emailWarningSubject = configuration["Watcher:WarningEmailSubject"] ?? "GRAFT Service Warninig (_service_name_)"; _emailRestoreSubject = configuration["Watcher:RestoreEmailSubject"] ?? "GRAFT Service Restore (_service_name_)"; _sendErrorEmail = Convert.ToBoolean(configuration[$"{Name}:SendErrorEmail"] ?? "true"); _sendWarningEmail = Convert.ToBoolean(configuration[$"{Name}:SendWarningEmail"] ?? "true"); _sendRestoreEmail = Convert.ToBoolean(configuration[$"{Name}:SendRestoreEmail"] ?? "true"); if (_emailErrorSubject != null) { _emailErrorSubject = _emailErrorSubject.Replace("_service_name_", DisplayName); } if (_emailWarningSubject != null) { _emailWarningSubject = _emailWarningSubject.Replace("_service_name_", DisplayName); } if (_emailRestoreSubject != null) { _emailRestoreSubject = _emailRestoreSubject.Replace("_service_name_", DisplayName); } _logger.LogInformation("Service started"); }
protected void UpdateStopwatchMetrics(Stopwatch sw, bool success) { TotalRequestCount++; if (success) { SuccessfulRequestCount++; } else { FailedRequestCount++; } _lastElapsedTime = sw.Elapsed; if (sw.Elapsed < _minElapsedTime) { _minElapsedTime = sw.Elapsed; } if (sw.Elapsed > _maxElapsedTime) { _maxElapsedTime = sw.Elapsed; } _totalElapsedTime += sw.Elapsed; _avgElapsedTime = _totalElapsedTime / TotalRequestCount; Metrics["Total Requests"] = TotalRequestCount.ToString(); Metrics["Successful Requests"] = SuccessfulRequestCount.ToString(); Metrics["Failed Requests"] = FailedRequestCount.ToString(); Metrics["Last Request Time"] = _lastElapsedTime.ToString(); Metrics["Min Request Time"] = _minElapsedTime.ToString(); Metrics["Max Request Time"] = _maxElapsedTime.ToString(); Metrics["Avg Request Time"] = _avgElapsedTime.ToString(); Metrics["Total Request Time"] = _totalElapsedTime.ToString(); }