/// <summary> /// Initializes a new instance of the <see cref="AuditController"/> class. /// </summary> /// <param name="auditFilePath">The path to the XML audit file.</param> public AuditController(string auditFilePath) { _colAuditGroup = new AuditCollection(); TableTemplates = AuditUtils.GeTableTemplates(); LoadAuditGroup(auditFilePath); }
/// <summary> /// Adds the specified items. /// </summary> /// <param name="items">The items.</param> /// <returns>System.Int32[].</returns> public int[] Add(AuditCollection items) { ArrayList indexes = new ArrayList(); foreach (object item in items) { indexes.Add(this.List.Add(item)); } return((int[])(indexes.ToArray(typeof(int)))); }
/// <summary> /// Sends the audit report email. /// </summary> /// <param name="auditGroup">The audit group.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public static bool SendAuditUnitTestReportEmail(AuditCollection auditGroup) { var succeed = false; try { AuditReports report = new AuditReports(); string htmlBody = report.CreateUnitTestStyleReport(auditGroup); MailMessage message; var mailClient = CreateMailMessage(out message, auditGroup, htmlBody); mailClient.Send(message); succeed = true; } catch (SmtpException smtpEx) { StringBuilder sb = new StringBuilder(); sb.AppendLine(smtpEx.Message); if (smtpEx.InnerException != null) { sb.AppendLine(smtpEx.InnerException.Message); } succeed = false; var logger = GetFileLogger(); logger.Error(smtpEx, sb.ToString()); } catch (Exception ex) { succeed = false; var logger = GetFileLogger(); logger.Error(ex, ex.Message); } return(succeed); }
/// <summary> /// Sends the single audit failure email. /// </summary> /// <param name="auditGroup">The audit group.</param> /// <param name="failingAuditIndex">Index of the failing audit.</param> /// <param name="body">The body of the email.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public static bool SendSingleAuditFailureEmail(AuditCollection auditGroup, int failingAuditIndex, string body) { bool succeed = false; MailMessage message; var mailClient = CreateMailMessage(out message, auditGroup, body); if (!string.IsNullOrEmpty(auditGroup.EmailSubject)) { message.Subject = auditGroup.EmailSubject; } else { message.Subject = "Audit Failure - " + auditGroup[failingAuditIndex].Name; } try { mailClient.Send(message); succeed = true; } catch (SmtpException smtpEx) { StringBuilder sb = new StringBuilder(); sb.AppendLine(smtpEx.Message); if (smtpEx.InnerException != null) { sb.AppendLine(smtpEx.InnerException.Message); } succeed = false; var logger = GetFileLogger(); logger.Error(smtpEx, sb.ToString()); } return(succeed); }
private static SmtpClient CreateMailMessage(out MailMessage message, AuditCollection auditGroup, string body) { message = new MailMessage { IsBodyHtml = true, Subject = $"Audit Results for {auditGroup.AuditGroupName}" }; Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); string sourceEmailDescription = config.AppSettings.Settings["sourceEmailDescription"].Value; foreach (string recipient in auditGroup.EmailSubscribers) { message.To.Add(new MailAddress(recipient)); } if (auditGroup.EmailCarbonCopySubscribers != null) { // Carbon Copies - CC foreach (string ccemail in auditGroup.EmailCarbonCopySubscribers) { message.CC.Add(new MailAddress(ccemail)); } } if (auditGroup.EmailBlindCarbonCopySubscribers != null) { // Blind Carbon Copies - BCC foreach (string bccemail in auditGroup.EmailBlindCarbonCopySubscribers) { message.Bcc.Add(new MailAddress(bccemail)); } } message.Body = body; switch (auditGroup.EmailPriority) { case EmailPriorityEnum.Low: message.Priority = MailPriority.Low; break; case EmailPriorityEnum.Normal: message.Priority = MailPriority.Normal; break; case EmailPriorityEnum.High: message.Priority = MailPriority.High; break; default: message.Priority = MailPriority.Normal; break; } message.From = new MailAddress(auditGroup.SmtpSourceEmail, sourceEmailDescription); var smtpClient = new SmtpClient(); if (auditGroup.SmtpHasCredentials) { smtpClient.UseDefaultCredentials = false; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.Host = auditGroup.SmtpServerAddress; smtpClient.Port = auditGroup.SmtpPort; smtpClient.Credentials = new NetworkCredential(auditGroup.SmtpUserName, auditGroup.SmtpPassword); smtpClient.EnableSsl = auditGroup.SmtpUseSsl; } else { smtpClient.Host = auditGroup.SmtpServerAddress; } return(smtpClient); }
/// <summary> /// Creates the report. /// </summary> /// <param name="audits">The audits.</param> /// <returns>System.String.</returns> public string CreateUnitTestStyleReport(AuditCollection audits) { return(BuildHtmlBodyForUnitTestReportEmail(audits)); }
/// <summary> /// Builds the HTML body for unit test report email. /// </summary> /// <param name="auditGroup">The audit group.</param> /// <returns>System.String.</returns> public static string BuildHtmlBodyForUnitTestReportEmail(AuditCollection auditGroup) { var body = new StringBuilder(); body.AppendFormat("<h1>" + auditGroup.AuditGroupName + "</h1>"); body.Append("These audits ran at " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt", CultureInfo.InvariantCulture) + AuditUtils.HtmlBreak); body.Append("<b>This report was run on: " + auditGroup.ConnectionString.DatabaseServer + "</b>" + AuditUtils.HtmlBreak); StringBuilder database = new StringBuilder(); switch (auditGroup.ConnectionString.DatabaseProviderName) { case "mysql.data.mysqlclient": database.Append("<table><tr><td>"); database.Append( "<img src=https://cdn.rawgit.com/hectorsosajr/NDataAudit/72848767/images/32_MySQL.png>"); database.Append("</td><td>"); database.Append("Database Engine is Oracle MySQL"); database.Append("</td></tr></table>"); break; case "system.data.sqlclient": database.Append("<table><tr><td>"); database.Append( "<img src=https://cdn.rawgit.com/hectorsosajr/NDataAudit/87edd0dc/images/32_SQLServer.png>"); database.Append("</td><td>"); database.Append("Database Engine is Microsoft SQL Server"); database.Append("</td></tr></table>"); break; default: database.Append("Database Engine is UNKNOWN"); break; } body.Append(database + AuditUtils.HtmlBreak + AuditUtils.HtmlBreak); if (!string.IsNullOrEmpty(auditGroup.TemplateColorScheme.CssTableStyle)) { body.AppendLine("<style>"); body.AppendLine(auditGroup.TemplateColorScheme.CssTableStyle); body.AppendLine("</style>"); body.Append("<TABLE id=emailtable BORDER=1>"); body.Append("<TR>"); } else { body.Append("<TABLE BORDER=1 width=\"100%\">"); body.Append("<TR ALIGN='LEFT' style='white-space: nowrap;'>"); } if (!string.IsNullOrEmpty(auditGroup.TemplateColorScheme.CssTableStyle)) { body.Append("<TH>Status</TH>"); } else { body.Append("<TD style='white-space: nowrap;' bgcolor=\"" + auditGroup.TemplateColorScheme.HtmlHeaderBackgroundColor + "\"><B>"); body.Append("<font color=\"" + auditGroup.TemplateColorScheme.HtmlHeaderFontColor + "\">Status</font>"); body.Append("</B></TD>"); } if (!string.IsNullOrEmpty(auditGroup.TemplateColorScheme.CssTableStyle)) { body.Append("<TH>Audit Name</TH>"); } else { body.Append("<TD style='white-space: nowrap;' bgcolor=\"" + auditGroup.TemplateColorScheme.HtmlHeaderBackgroundColor + "\"><B>"); body.Append("<font color=\"" + auditGroup.TemplateColorScheme.HtmlHeaderFontColor + "\">Audit Name</font>"); body.Append("</B></TD>"); } body.Append("</TR>"); foreach (Audit currentAudit in auditGroup) { body.Append("<TR>"); // Status Icon string testResult; string sql = string.Empty; string errorMessage = string.Empty; string help = string.Empty; if (currentAudit.Result) { testResult = "<img src={pass}>"; } else { testResult = "<img src={fail}>"; sql = currentAudit.Test.SqlStatementToCheck.ToHtml(); errorMessage = currentAudit.Test.FailedMessage; help = currentAudit.Test.Instructions; } if (!string.IsNullOrEmpty(auditGroup.TemplateColorScheme.CssTableStyle)) { body.Append("<TD>"); body.Append(testResult); } else { body.Append("<TD style='white-space: nowrap;'>"); body.Append(testResult); } body.Append("</TD>"); // Audit Name if (!string.IsNullOrEmpty(auditGroup.TemplateColorScheme.CssTableStyle)) { body.Append("<TD>"); body.Append(currentAudit.Name); } else { body.Append("<TD style='white-space: nowrap;'>"); body.Append(currentAudit.Name); } body.Append("</TD>"); body.Append("</TR>"); // Failed Audit Information if (!currentAudit.Result) { // Info icon if (!string.IsNullOrEmpty(auditGroup.TemplateColorScheme.CssTableStyle)) { body.Append("<TD>"); body.Append("<img src={info}>"); } else { body.Append("<TD style='white-space: nowrap;'>"); body.Append("<img src={info}>"); } body.Append("</TD>"); // Failure Information body.Append("<TD>"); body.Append("<TABLE BORDER=1>"); // Error Message Icon body.Append("<TR ALIGN='LEFT' style='white-space: nowrap;'>"); body.Append("<TD>"); body.Append("<img src={error}>"); body.Append("</TD>"); // Error Message Text body.Append("<TD>"); body.Append(errorMessage); body.Append("</TD>"); body.Append("</TR>"); // SQL Icon body.Append("<TR ALIGN='LEFT' style='white-space: nowrap;'>"); body.Append("<TD>"); body.Append("<img src={sql}>"); body.Append("</TD>"); // SQL Text body.Append("<TD>"); body.Append(sql); body.Append("</TD>"); body.Append("</TR>"); // Help Icon body.Append("<TR ALIGN='LEFT' style='white-space: nowrap;'>"); body.Append("<TD>"); body.Append("<img src={help}>"); body.Append("</TD>"); // Comment or Instruction Text body.Append("<TD>"); body.Append(help); body.Append("</TD>"); body.Append("</TR>"); body.Append("</TABLE>"); body.Append("</TD>"); } } body.Append("</TABLE>"); string htmlBody = body.ToString(); htmlBody = htmlBody.Replace("{pass}", "https://cdn.rawgit.com/hectorsosajr/NDataAudit/c74445b1/images/32_database-check.png"); htmlBody = htmlBody.Replace("{fail}", "https://cdn.rawgit.com/hectorsosajr/NDataAudit/c74445b1/images/32_database-fail.png"); htmlBody = htmlBody.Replace("{sql}", "https://cdn.rawgit.com/hectorsosajr/NDataAudit/72a07bd7/images/32_database-sql.png"); htmlBody = htmlBody.Replace("{info}", "https://cdn.rawgit.com/hectorsosajr/NDataAudit/72a07bd7/images/32_database-info.png"); htmlBody = htmlBody.Replace("{error}", "https://cdn.rawgit.com/hectorsosajr/NDataAudit/72a07bd7/images/32_database-error.png"); htmlBody = htmlBody.Replace("{help}", "https://cdn.rawgit.com/hectorsosajr/NDataAudit/72a07bd7/images/32_database-help.png"); htmlBody = htmlBody.Replace("\r\n", string.Empty); htmlBody = htmlBody.Replace(@"\", string.Empty); return(htmlBody); }
/// <summary> /// Sends the output of the <see cref="Audit"/> result. Currently only through emails. /// </summary> /// <param name="auditGroup">The list of <see cref="Audit"/>s that were tested.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public static bool SendResult(AuditCollection auditGroup) { var succeed = false; try { string htmlBody; switch (auditGroup.AuditResultOutputType) { case OutputType.UnitTest: OutputUnitTest unitTest = new OutputUnitTest(auditGroup); htmlBody = unitTest.CreateOutputBody(); break; case OutputType.Alert: OutputAlert alert = new OutputAlert(auditGroup); htmlBody = alert.CreateOutputBody(); break; case OutputType.Audit: OutputAudit audit = new OutputAudit(auditGroup); htmlBody = audit.CreateOutputBody(); break; case OutputType.Report: OutputReport report = new OutputReport(auditGroup); htmlBody = report.CreateOutputBody(); break; default: throw new ArgumentOutOfRangeException(); } var mailClient = CreateMailMessage(out var message, auditGroup, htmlBody); mailClient.Send(message); succeed = true; } catch (SmtpException smtpEx) { StringBuilder sb = new StringBuilder(); sb.AppendLine(smtpEx.Message); if (smtpEx.InnerException != null) { sb.AppendLine(smtpEx.InnerException.Message); } succeed = false; var logger = GetFileLogger(); logger.Error(smtpEx, sb.ToString()); } catch (Exception ex) { succeed = false; var logger = GetFileLogger(); logger.Error(ex, ex.Message); } return(succeed); }
/// <summary> /// Constructor that lets you load an AuditCollection. /// </summary> /// <param name="colAudits">An AuditCollection object</param> public AuditTesting(AuditCollection colAudits) { _colAudits = colAudits; _providers = new DbProviderCache(); }