private string Build( MessageType type, string path, ComponentStatus status) { using (_logger.Scope("Getting contents for message of type {MessageType} with path {ComponentPath} and status {ComponentStatus}.", type, path, status)) { if (!_messageTypeToMessageTemplate.TryGetValue(type, out string messageTemplate)) { throw new ArgumentException("Could not find a template for type.", nameof(type)); } _logger.LogInformation("Using template {MessageTemplate}.", messageTemplate); var nameString = GetName(path); _logger.LogInformation("Using {ComponentName} for name of component.", nameString); var actionDescription = GetActionDescriptionFromPath(path); if (actionDescription == null) { throw new ArgumentException("Could not find an action description for path.", nameof(path)); } var statusString = status.ToString().ToLowerInvariant(); var contents = string.Format(messageTemplate, nameString, statusString, actionDescription); _logger.LogInformation("Returned {Contents} for contents of message.", contents); return(contents); } }
public bool CronJobInfoLog(object message, ServiceType serviceType, dynamic request = null, string errorCode = "", string identity = "", [CallerFilePath] string fileName = "", [CallerMemberName] string methodName = "", ComponentStatus componentStatus = ComponentStatus.None, LogSeverity logSeverity = LogSeverity.CronJobInfo) { if (!_logging) { return(false); } try { InfoLoggerModel infoLoggerModel = new InfoLoggerModel { Date = DateTime.UtcNow, Severity = logSeverity.ToString(), Logger = serviceType.ToString(), File = fileName.ToFileName(), Method = methodName, Identity = !string.IsNullOrEmpty(identity) ? identity : Convert.ToString(CommonMethods.GetValueFromDynamic(request, "UserID")), Message = Convert.ToString(message), ApplicationCode = _applicationCode ?? string.Empty, ErrorCode = errorCode, MessageData = request != null?JsonConvert.SerializeObject(request, Formatting.None) : string.Empty, ComponentStatus = componentStatus != ComponentStatus.None ? componentStatus.ToString() : string.Empty }; return(_eventBus.Publish(infoLoggerModel)); } catch (Exception ex) { return(false); } }
private static async Task DoMonitorCheck(CachetClient Cachet, Monitor monitor) { ComponentStatus componentStatus = 0; switch (monitor.Type) { case MonitorType.PORT: { using TcpClient tcpClient = new TcpClient(); try { tcpClient.Connect(monitor.Target, monitor.Settings.Port); try { componentStatus = ComponentStatus.Operational; await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent { Status = componentStatus, }); Log.Debug("PortMonitorCheck", "Sent to Cachet successfully"); } catch (Exception ex) { Log.Error("PortMonitorCheck", ex.Message, ex); } } catch (Exception ex) { Log.Error("PortMonitorCheck", ex.Message, ex); try { componentStatus = ComponentStatus.MajorOutage; await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent { Status = componentStatus, }); Log.Debug("PortMonitorCheck", "Sent to Cachet successfully"); } catch (Exception ex2) { Log.Error("PortMonitorCheck", ex2.Message, ex2); } } } break; case MonitorType.ICMP: { Ping ping = new Ping(); PingOptions options = new PingOptions { DontFragment = true, Ttl = monitor.Settings.TTL }; PingReply reply = ping.Send(monitor.Target, monitor.Timeout, null, options); try { await Cachet.AddMetricPointAsync(monitor.MetricId, new PostMetricPoint { Value = (int)reply.RoundtripTime }); Log.Debug("IPMonitorCheck", "Sent to Cachet successfully"); } catch (Exception ex) { Log.Error("IPMonitorCheck", ex.Message, ex); } if (reply.Status == IPStatus.Success) { try { componentStatus = ComponentStatus.Operational; await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent { Status = componentStatus, }); Log.Debug("IPMonitorCheck", "Sent to Cachet successfully"); } catch (Exception ex) { Log.Error("IPMonitorCheck", ex.Message, ex); } } if (reply.RoundtripTime >= monitor.Timeout) { try { componentStatus = ComponentStatus.PerformanceIssues; await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent { Status = componentStatus, }); Log.Debug("IPMonitorCheck", "Sent to Cachet successfully"); } catch (Exception ex) { Log.Error("IPMonitorCheck", ex.Message, ex); } } if (reply.Status != IPStatus.Success) { if (reply.Status == IPStatus.TimedOut) { try { componentStatus = ComponentStatus.PartialOutage; await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent { Status = componentStatus, }); Log.Debug("IPMonitorCheck", "Sent to Cachet successfully"); } catch (Exception ex) { Log.Error("IPMonitorCheck", ex.Message, ex); } } else { try { componentStatus = ComponentStatus.MajorOutage; await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent { Status = componentStatus, }); Log.Debug("IPMonitorCheck", "Sent to Cachet successfully"); } catch (Exception ex) { Log.Error("IPMonitorCheck", ex.Message, ex); } } } } break; case MonitorType.HTTP: { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(monitor.Target); request.Timeout = monitor.Timeout * 1000; Stopwatch timer = new Stopwatch(); timer.Start(); try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); timer.Stop(); if (response.StatusCode == (HttpStatusCode)monitor.Settings.ExpectedStatusCode) { try { componentStatus = ComponentStatus.Operational; await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent { Status = componentStatus, }); Log.Debug("WebMonitorCheck", "Sent to Cachet successfully"); } catch (Exception ex) { Log.Error("WebMonitorCheck", ex.Message, ex); } } else { try { componentStatus = ComponentStatus.PartialOutage; await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent { Status = componentStatus, }); Log.Debug("WebMonitorCheck", "Sent to Cachet successfully"); } catch (Exception ex) { Log.Error("WebMonitorCheck", ex.Message, ex); } } response.Close(); } catch (WebException ex) { timer.Stop(); Log.Warning("WebMonitorChcek", ex.Message, ex); if (ex.Status == WebExceptionStatus.ProtocolError) { if (ex.Response is HttpWebResponse response) { if (response.StatusCode == (HttpStatusCode)monitor.Settings.ExpectedStatusCode) { try { componentStatus = ComponentStatus.Operational; await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent { Status = componentStatus, }); Log.Debug("WebMonitorCheck", "Sent to Cachet successfully"); } catch (Exception ex2) { Log.Error("WebMonitorCheck", ex2.Message, ex2); } } } } else { if (ex.Status == WebExceptionStatus.Timeout) { try { componentStatus = ComponentStatus.PerformanceIssues; await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent { Status = componentStatus, }); Log.Debug("WebMonitorCheck", "Sent to Cachet successfully"); } catch (Exception ex2) { Log.Error("WebMonitorCheck", ex2.Message, ex2); } } else { try { componentStatus = ComponentStatus.MajorOutage; await Cachet.UpdateComponentAsync(monitor.ComponentId, new PutComponent { Status = componentStatus, }); Log.Debug("WebMonitorCheck", "Sent to Cachet successfully"); } catch (Exception ex2) { Log.Error("WebMonitorCheck", ex2.Message, ex2); } } } } await Cachet.AddMetricPointAsync(monitor.MetricId, new PostMetricPoint { Value = (int)timer.ElapsedMilliseconds }); } break; } Log.Verbose("DoMonitorCheck", $"Ran check on \"{monitor.Name}\" Status: {componentStatus.ToString()}"); }