Esempio n. 1
0
        /// <summary>
        /// Saves entries file.
        /// </summary>
        private static void WriteEntriesFile()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                using (SimpleJsonWriter writer = new SimpleJsonWriter(sb))
                {
                    writer.WriteStartObject();
                    writer.WriteStartArray("entries");
                    foreach (Entry entry in _entries)
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyValue("ip", entry.IP);
                        writer.WritePropertyValue("host", entry.Host);
                        writer.WritePropertyValue("mac", entry.Mac);
                        writer.WriteEndObject();
                    }
                    writer.WriteEndArray();
                    writer.WriteEndObject();
                }

                Console.WriteLine($"Writing file {EntriesFile}");
                File.WriteAllText(EntriesFile, sb.ToString());
            }
            catch (Exception ex)
            {
                throw new Exception($"Unable to save file {EntriesFile}", ex);
            }
        }
Esempio n. 2
0
            public string ToJson()
            {
                StringBuilder sb = new StringBuilder();

                using (SimpleJsonWriter writer = new SimpleJsonWriter(sb, true))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyValue("serial", Serial);
                    writer.WritePropertyValue("name", Name);
                    writer.WritePropertyValue("version", Version);
                    writer.WritePropertyValue("httpPort", HttpPort);
                    writer.WritePropertyValue("runningSecs", (int)RunningTime.TotalSeconds);
                    writer.WritePropertyValue("serviceState", ServiceState);
                    writer.WriteStartArray("interfaces");
                    foreach (Interface i in Interfaces)
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyValue("name", i.Name);
                        writer.WritePropertyValue("physical", i.PhysicalAddress);
                        writer.WritePropertyValue("internet", i.InternetAddress);
                        writer.WriteEndObject();
                    }
                    writer.WriteEndArray();
                    writer.WriteEndObject();
                }
                return(sb.ToString());
            }
Esempio n. 3
0
        /// <summary>
        /// Writes runtime statistics.
        /// </summary>
        public void WriteRuntimeStatistics(SimpleJsonWriter writer)
        {
            try
            {
                List <ErrorCacheItem> errorStats = GetErrorReport();

                writer.WriteStartArray("errorStats");
                foreach (ErrorCacheItem errorItem in errorStats)
                {
                    writer.WriteStartObject();
                    writer.WritePropertyValue("count", errorItem.Count);
                    writer.WritePropertyValue("cpm", Math.Round(errorItem.CountPerMinute, 1));
                    writer.WritePropertyValue("type", errorItem.Error.Type ?? "");
                    writer.WritePropertyValue("message", errorItem.Error.Message ?? "");
                    writer.WritePropertyValue("stackTrace", errorItem.Error.StackTrace ?? "");
                    writer.WriteEndObject();
                }
                writer.WriteEndArray();
            }
            catch (Exception ex)
            {
                if (_errorHandler != null)
                {
                    _errorHandler.LogError(ex);
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Writes runtime statistics.
        /// </summary>
        public void WriteRuntimeStatistics(SimpleJsonWriter writer)
        {
            try
            {
                var          now    = DateTime.Now;
                var          state  = ServiceStateType.Up;
                var          health = ServiceHealthType.Good;
                List <Alert> alerts = null;
                lock (_lock)
                {
                    state = _state;
                    foreach (Alert a in _alerts)
                    {
                        if (a.Type > health)
                        {
                            health = a.Type;
                        }
                    }
                    if (health > ServiceHealthType.Good)
                    {
                        alerts = new List <Alert>();
                        foreach (Alert a in _alerts)
                        {
                            alerts.Add(a);
                        }
                    }
                }

                writer.WriteStartObject("serviceState");
                writer.WritePropertyValue("state", ((Int32)state) + "-" + state.ToString());
                writer.WritePropertyValue("health", ((Int32)health) + "-" + health.ToString());
                if (alerts != null)
                {
                    writer.WriteStartArray("alerts");
                    foreach (Alert a in alerts)
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyValue("type", ((Int32)a.Type) + "-" + a.Type.ToString());
                        writer.WritePropertyValue("message", a.Message ?? "");
                        writer.WritePropertyValue("time", a.Time.ToString());
                        writer.WritePropertyValue("expiration", a.Expiration == DateTime.MaxValue ? "max" : ((Int32)(((TimeSpan)(a.Expiration - now)).TotalMinutes)).ToString());
                        writer.WriteEndObject();
                    }
                    writer.WriteEndArray();
                }
                writer.WriteEndObject();
            }
            catch (Exception ex)
            {
                _errorHandler?.LogError(ex);
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Writes the 'errors' object for all commands.
 /// </summary>
 public void WriteErrorsObject(SimpleJsonWriter writer, List <Exception> errors)
 {
     writer.WriteStartArray("errors");
     if ((errors != null) && (errors.Count > 0))
     {
         for (int i = 0; i < errors.Count; i++)
         {
             Exception ex = errors[i];
             if (!String.IsNullOrEmpty(ex.Message))
             {
                 writer.WriteStartObject();
                 writer.WritePropertyValue("type", ex.GetType().ToString());
                 writer.WritePropertyValue("message", CleanErrorString(ex.Message));
                 writer.WritePropertyValue("stack", CleanErrorString(ex.StackTrace ?? ""));
                 writer.WriteEndObject();
             }
         }
     }
     writer.WriteEndArray();
 }
Esempio n. 6
0
        /// <summary>
        /// Writes runtime statistics.
        /// </summary>
        public void WriteRuntimeStatistics(SimpleJsonWriter writer)
        {
            try
            {
                GetSummary(
                    out List <OperationRollup> operations,
                    out List <OperationRollup> commands,
                    out List <TaskStats> runningTasks,
                    out List <TaskStats> completedTasks);

                writer.WriteStartObject("serviceStats");
                writer.WriteStartArray("operations");
                foreach (OperationRollup r in operations)
                {
                    writer.WriteStartObject();
                    writer.WritePropertyValue("name", r.Name ?? String.Empty);
                    writer.WritePropertyValue("count", r.Count);
                    writer.WritePropertyValue("elapsedAvg", Math.Round(r.ElapsedAvg, 1));
                    writer.WritePropertyValue("elapsedMin", r.ElapsedMin);
                    writer.WritePropertyValue("elapsedMax", r.ElapsedMax);
                    writer.WritePropertyValue("elapsedSum", r.ElapsedSum);
                    writer.WritePropertyValue("cps", Math.Round(r.CPS, 2));
                    writer.WriteEndObject();
                }
                writer.WriteEndArray();
                writer.WriteStartArray("commands");
                foreach (OperationRollup r in commands)
                {
                    writer.WriteStartObject();
                    writer.WritePropertyValue("name", r.Name ?? String.Empty);
                    writer.WritePropertyValue("count", r.Count);
                    writer.WritePropertyValue("elapsedAvg", Math.Round(r.ElapsedAvg, 1));
                    writer.WritePropertyValue("elapsedMin", r.ElapsedMin);
                    writer.WritePropertyValue("elapsedMax", r.ElapsedMax);
                    writer.WritePropertyValue("elapsedSum", r.ElapsedSum);
                    writer.WritePropertyValue("cps", Math.Round(r.CPS, 2));
                    writer.WriteEndObject();
                }
                writer.WriteEndArray();
                writer.WriteStartArray("completedTasks");
                foreach (TaskStats t in completedTasks)
                {
                    writer.WriteStartObject();
                    writer.WritePropertyValue("name", t.Name ?? String.Empty);
                    writer.WritePropertyValue("ips", Math.Round(t.IterationsPerSecond, 1));
                    writer.WritePropertyValue("elapsed", t.Elapsed.ToShortString());
                    writer.WriteEndObject();
                }
                writer.WriteEndArray();
                writer.WriteStartArray("runningTasks");
                foreach (TaskStats t in runningTasks)
                {
                    writer.WriteStartObject();
                    writer.WritePropertyValue("name", t.Name ?? String.Empty);
                    writer.WritePropertyValue("ips", Math.Round(t.IterationsPerSecond, 1));
                    writer.WritePropertyValue("percentComplete", Math.Round(t.PercentComplete, 1));
                    writer.WritePropertyValue("eta", t.ETA.ToShortString());
                    writer.WritePropertyValue("elapsed", t.Elapsed.ToShortString());
                    writer.WriteEndObject();
                }
                writer.WriteEndArray();
                writer.WriteEndObject();
            }
            catch (Exception ex)
            {
                if (_errorHandler != null)
                {
                    _errorHandler.LogError(ex);
                }
            }
        }