private static void CheckForEmail(ErrorMessage errMsg) { List<DateTime> currentErrorList; // Holds current error list DateTime cutOff = errMsg.Date.AddMinutes(-5); // Holds current cut off time // Add a list for error type if it currently does not exist if (!messages.ContainsKey(errMsg.FullKey)) messages.Add(errMsg.FullKey, new List<DateTime>()); // Selected the list that matches current error type currentErrorList = messages[errMsg.FullKey]; // Remove entries that are 5 min older then current error for (int i = currentErrorList.Count - 1; i > -1; --i) if (currentErrorList[i] < cutOff) currentErrorList.RemoveAt(i); // Add current error to list. currentErrorList.Add(errMsg.Date); // Check to see if list meets threshold required // if it is send email and reset list for current error type if (currentErrorList.Count >= CommandLineSettings.Threshold) { currentErrorList.Clear(); SendEmail(errMsg); } }
private static void SendEmail(ErrorMessage errMsg) { // Command line argument must the the SMTP host. SmtpClient client = new SmtpClient(Settings.Host, Settings.Port); client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(Settings.SendFrom, Settings.Password); // Specify the e-mail sender. // Create a mailing address that includes a UTF8 character // in the display name. MailAddress from = new MailAddress(Settings.SendFrom); // Set destinations for the e-mail message. MailAddress to = new MailAddress(Settings.SendTo); // Specify the message content. MailMessage message = new MailMessage(from, to); message.Body = errMsg.ToString(); message.Subject = errMsg.Description; // Set the method that is called back when the send operation ends. client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); // Sending message client.Send(message); // Clean up. message.Dispose(); }
private static void ParseEventLog() { try { // Check to see that file exists. if (System.IO.File.Exists(CommandLineSettings.LogFile)) { StreamReader input; // Open log file for reading and allow access to other programs to continue to read and write to file using (input = new StreamReader(new FileStream(CommandLineSettings.LogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { while (!input.EndOfStream) { // Get next line string message = input.ReadLine(); // Parse error message ErrorMessage errMsg = new ErrorMessage(message); // Check that message is ok if (!errMsg.ParsingError) { // Add error to list of similar messages and check for threshold CheckForEmail(errMsg); } else Warn("Unable to parse record - '" + message + "'"); } } } else Error("Log file '" + CommandLineSettings.LogFile + "' does not exist. Program terminating."); } catch (Exception ex) { Error("Error encountered while parsing file: \r\n" + ex.ToString()); } }