private void DoRecurringTransact(object _) { BeginLogging(); try { var metrics = _gatherer.GatherMetrics(); _dbLog.InfoFormat("Application node has {0} metrics", metrics.Count()); var alerts = _gatherer.NewAlerts(); _log.InfoFormat("Application node sending {0} alerts", alerts.Count()); var newState = ApplicationNodeStates.Running; LoggingConfiguration newLogConfig = null; DoTransactNodeInfo(metrics, DateTime.UtcNow, alerts, _gatherer.ActivityLevel, out newState, out newLogConfig); if (newState != _state && null != StateChanged) { _log.WarnFormat("Application node state: {0}", newState.EnumName()); _state = newState; StateChanged(this, new ApplicationNodeStateEvent { State = newState }); } _logConfig.MaybeReconfigureLogging(newLogConfig); if (alerts.Any()) { var appLog = new System.Diagnostics.EventLog(); foreach (var alert in alerts) { appLog.Source = Process.GetCurrentProcess().ProcessName; var jsonDetail = JsonConvert.SerializeObject(alert.Detail); var strEv = string.Format("Operational Event {0}:\n{1}", alert.Kind, jsonDetail); appLog.WriteEntry(strEv); } } } catch (Exception ex) { _log.Error(ex.TraceInformation()); _alert.RaiseAlert(ApplicationAlertKind.Unknown, ex.TraceInformation()); } }
public void DoTransactNodeInfo( IEnumerable <NodeMetric> currentMetrics, DateTime metricsCollectionTime, IEnumerable <NodeAlert> newAlerts, int activityLevel, out ApplicationNodeStates desiredState, out LoggingConfiguration desiredLogging) { desiredState = ApplicationNodeStates.Running; desiredLogging = null; try { using (var dc = DocumentStoreLocator.Resolve(DocumentStoreLocator.RootLocation)) { // try to load existing node var appNode = dc.Load <ApplicationNode>(_reg.Id); // create new if necessary bool created = false; if (null == appNode) { _log.Warn("Creating application node record"); created = true; appNode = new ApplicationNode { Id = _reg.Id, ComponentType = _reg.ComponentType, MachineName = Environment.MachineName, State = ApplicationNodeStates.Running, Version = _reg.Version, IPAddress = GetLocalIP(), LoggingConfiguration = new LoggingConfiguration { ClassFilter = "none", File = _logFilePath, LogLevel = 0 } }; } // update node data desiredLogging = appNode.LoggingConfiguration; desiredState = appNode.State; appNode.LastPing = DateTime.UtcNow; appNode.MetricCollectionTime = metricsCollectionTime; appNode.Metrics = currentMetrics.ToList(); appNode.Alerts.AddRange(newAlerts); appNode.ActivityLevel = activityLevel; if (created) { dc.Store(appNode); } dc.SaveChanges(); } } catch (ConcurrencyException) { // just get it next time } catch (Exception ex) { _log.Error(ex.TraceInformation()); _alert.RaiseAlert(ApplicationAlertKind.Unknown, ex.TraceInformation()); } }