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, AppServerConfigurator.SerializerSettings)); } }
internal void ComputeStatisticks(ActionResult result) { if (AppServerConfigurator.DisableStatisticsCalculating) { return; } if (result.Type == 1) { return; } StringBuilder json = new StringBuilder(); using (StringWriter sw = new StringWriter(json)) { using (JsonWriter jw = new JsonTextWriter(sw)) { JsonSerializer js = new JsonSerializer(); js.ApplyCustomSettings(); js.Serialize(jw, result.Content); } } int bufferSize = coreServer.GetConfiguration().BufferSize; byte[] bytes = encoder.ConvertToByteArray(json.ToString()); int lenght = bytes.Length; double percentBufferUsed = (lenght / (double)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: {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); } }