예제 #1
0
        internal void ComputeStatisticks(ActionResult result)
        {
            if (result.Type == 1)
            {
                return;
            }
            string json = JsonConvert.SerializeObject(result.Content);

            var    bytes             = Server.GlobalInstance.ServerEncoding.GetBytes(json);
            var    lenght            = bytes.Length;
            double percentBufferUsed = (lenght / (double)Server.GlobalInstance.BufferSize) * 100;

            result.ResponseLenght = lenght;
            result.PercentUsage   = percentBufferUsed;

            if (percentBufferUsed >= 100)
            {
                string msg = $@"
The action response on the controller is using around {percentBufferUsed.ToString("N2")}% of the buffer quota configured on the server.
Server Buffer Size: {Server.GlobalInstance.BufferSize}
Response Size:      {lenght}
Operation has stopped.";

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(msg);
                Console.ForegroundColor = ConsoleColor.Gray;

                var alert = new ServerAlert
                {
                    Action     = Action,
                    Controller = Controller.GetType().Name,
                    Date       = DateTime.Now,
                    Message    = msg
                };

                ServerAlertManager.CreateAlert(alert);
                throw new Exception(msg);
            }

            if (percentBufferUsed >= 80)
            {
                string msg = $"\nThe action response on the controller is using around {percentBufferUsed.ToString("N2")}% of the buffer quota configured on the server. Review your code as soon as possible before the server collapses and begins to give incomplete responses to connected clients.";
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(msg);
                Console.ForegroundColor = ConsoleColor.Gray;

                var alert = new ServerAlert
                {
                    Action     = Action,
                    Controller = Controller.GetType().Name,
                    Date       = DateTime.Now,
                    Message    = msg
                };

                ServerAlertManager.CreateAlert(alert);
            }
        }
예제 #2
0
        public static void CreateAlert(ServerAlert alert)
        {
            lock (writeLock)
            {
                string file   = @".\ServerAlerts.json";
                var    alerts = Load();

                if (alerts.Count > 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"\nYour server has {alerts.Count} alerts!");
                    Console.ForegroundColor = ConsoleColor.Gray;
                }

                alerts.Add(alert);
                File.WriteAllText(file, JsonConvert.SerializeObject(alerts));
            }
        }