private static void SendMailLog(string Body, InfosLog p_InfosLog) { string Subject = string.Empty; MailPriority v_MailPriority = MailPriority.Normal; switch (p_InfosLog.Severity) { case SEVERITY_ERROR: Subject = "[ERROR]"; v_MailPriority = MailPriority.Normal; break; case SEVERITY_WARNING: Subject = "[Warning]"; v_MailPriority = MailPriority.Low; break; case SEVERITY_INFO: Subject = "[Info]"; v_MailPriority = MailPriority.Low; break; } if (p_InfosLog.IsHighLevel == true) { v_MailPriority = MailPriority.High; } string InfosSubject = string.Empty; if (HttpContext.Current.Handler != null) { TextInfo textInfo = Thread.CurrentThread.CurrentCulture.TextInfo; InfosSubject = textInfo.ToTitleCase(HttpContext.Current.Handler.ToString().Replace("ASP.", string.Empty).Replace("_aspx", string.Empty).Replace("_", ">")); } if (p_InfosLog.IsDisplayPachetInfo == true) { Subject += " [x" + p_InfosLog.CounterGroup + "] (" + p_InfosLog.CounterTotalGroup + ")"; } if (InfosSubject != string.Empty) { Subject += " Alert in " + InfosSubject; } else { Subject += " Alert in Website"; } #region Send Log Mails //TODO: KT : Create the items in web config file // Get Config Mail string MailFrom = (string)ConfigurationManager.AppSettings["Log_MailFrom"]; string MailFromDisplayName = (string)ConfigurationManager.AppSettings["Log_MailFromDisplayName"]; string MailTo = (string)ConfigurationManager.AppSettings["Log_MailTo"]; string MailBc = (string)ConfigurationManager.AppSettings["Log_MailCc"]; if (!string.IsNullOrEmpty(MailFrom) && !string.IsNullOrEmpty(MailTo)) { // Get Subject & Body string MailSubject = Subject; string MailBody = Body; // Send Mail MailMessage MyMessage = new MailMessage(); MailAddress MyAddressFrom = new MailAddress(MailFrom, MailFromDisplayName); // + " [" + (string)ConfigurationManager.AppSettings["ServerName"] + "]"); MyMessage.From = MyAddressFrom; Regex v_Regex = new Regex("[;]"); string[] v_TabEmail = v_Regex.Split(MailTo); foreach (string v_Email in v_TabEmail) { MailAddress MyAddressTo = new MailAddress(v_Email); MyMessage.To.Add(MyAddressTo); } if (!string.IsNullOrEmpty(MailBc)) { string[] v_TabEmailBc = v_Regex.Split(MailBc); foreach (string v_EmailBc in v_TabEmailBc) { MailAddress MyAddressCc = new MailAddress(v_EmailBc); MyMessage.CC.Add(MyAddressCc); } } MyMessage.Priority = v_MailPriority; MyMessage.Subject = MailSubject; MyMessage.Body = MailBody; SendMail Sender = new SendMail(); Sender.Send(MyMessage); } #endregion }
private static void LoggerMail(string MessageLog, string KeyMessageException, string p_Severity) { #region Initialization DateTime NewDateTimeNow = DateTime.Now; // Get Configuration in web.config int LapTimeCritical = Convert.ToInt32(ConfigurationManager.AppSettings["Log_LapTimeCritical"]); int CriticalQuantityMail = Convert.ToInt32(ConfigurationManager.AppSettings["Log_CriticalQuantityMail"]); int QuantityMailInformBeforeCritical = Convert.ToInt32(ConfigurationManager.AppSettings["Log_QuantityMailInformBeforeCritical"]); int FrequenceMailToSend = Convert.ToInt32(ConfigurationManager.AppSettings["Log_CriticalFrequenceMail"]); #endregion #region Purge Logs stock in Application ArrayList v_ArrayListCodeDelete = new ArrayList(); foreach (string CodeLog in Log.LogList.Keys) { DateTime v_DateTimeLog = Log.LogList[CodeLog].LastDateTime; // If date of Log is out of date if (new TimeSpan(NewDateTimeNow.Ticks - v_DateTimeLog.Ticks).TotalSeconds > LapTimeCritical) { // If Log not send by mail (Purge) if (Log.LogList[CodeLog].IsMailSend == false) { string MessagePurge = MessageLog; // Send mail Log.SendMailLog(MessagePurge, Log.LogList[CodeLog]); } // Stock Log to delete v_ArrayListCodeDelete.Add(CodeLog); } } // Delete all Log out of date foreach (string CodeLog in v_ArrayListCodeDelete) { Log.LogList.Remove(CodeLog); } #endregion #region Log Error / Warning / Infos Mail InfosLog CurrentInfoLog; // if MessageLog exist in Application Object 'LogList' if (Log.LogList.ContainsKey(KeyMessageException)) { CurrentInfoLog = Log.LogList[KeyMessageException]; // Update CurrentLog CurrentInfoLog.CounterTotalGroup += 1; DateTime v_LastDateTime = CurrentInfoLog.LastDateTime; // if Date Log is NOT out-of-date if (new TimeSpan(NewDateTimeNow.Ticks - v_LastDateTime.Ticks).TotalSeconds < LapTimeCritical) { // if Quantity Mail send is equal Critical Quantity Mail -> Start High Level if ((CurrentInfoLog.CounterTotalGroup > (CriticalQuantityMail - QuantityMailInformBeforeCritical)) && (CurrentInfoLog.CounterTotalGroup <= CriticalQuantityMail)) { CurrentInfoLog.IsHighLevel = true; } // if Quantity Mail send is higher to Critical Quantity if (CurrentInfoLog.CounterTotalGroup > CriticalQuantityMail) { CurrentInfoLog.CounterGroup += 1; CurrentInfoLog.IsHighLevel = true; // if Frequency Mail is not reached, don't send if (((CurrentInfoLog.CounterTotalGroup - CriticalQuantityMail) % FrequenceMailToSend) != 0) { CurrentInfoLog.IsMailSend = false; CurrentInfoLog.IsDisplayPachetInfo = true; } else { CurrentInfoLog.IsMailSend = true; } } CurrentInfoLog.LastDateTime = NewDateTimeNow; } } else // Add new Log { CurrentInfoLog = new InfosLog(p_Severity); Log.LogList.Add(KeyMessageException, CurrentInfoLog); } if (CurrentInfoLog.IsMailSend == true) { Log.SendMailLog(MessageLog, CurrentInfoLog); // Reinit CounterGroup Log CurrentInfoLog.CounterGroup = 0; } #endregion }