public void Run(AlertPolicy alertPolicy) { foreach (var logId in alertPolicy.Logs) { var elmahResponse = _apiClient.GetMessages(logId, alertPolicy.FilterQuery); var alertSummary = CreateAlertSummary(elmahResponse, alertPolicy.PolicyName, logId); SetTimeDiffString(elmahResponse, alertSummary); if (alertPolicy.WhenFunc(elmahResponse.Messages)) { if (alertPolicy.AlertStarted == null || (DateTime.Now - alertPolicy.AlertStarted).Value.TotalMinutes > _realertAfterMinutes) { alertPolicy.AlertStarted = DateTime.Now; alertPolicy.AlertChannels.ForEach(p => p.Alert(alertSummary)); } } else { if (alertPolicy.AlertStarted != null) { alertPolicy.AlertChannels.ForEach( p => p.Close(alertPolicy.PolicyName, (int)(DateTime.Now - alertPolicy.AlertStarted.Value).TotalMinutes)); alertPolicy.AlertStarted = null; } } } }
public static IAlertSetting Create(string policyName, IApiClient client = null, int realertAfterMinutes = 60) { var instance = new AlertPolicy(new AlertRunner(client ?? new ElmahIoApiClient(), realertAfterMinutes)) { PolicyName = policyName }; return(instance); }