TFS_ANTIGUO/SIGPC/wcfSigdd/AyAEM/Exceptions/ExceptionPublisher.cs

115 lines
4.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Text;
namespace AyAEM.ExceptionManagement.Exceptions
{
public class ExceptionPublisher : IExceptionPublisher
{
private string m_LogName = String.Format("{0}ErrorLog.txt", AppDomain.CurrentDomain.BaseDirectory);
private string strFromMail = "", strToMail = "", strSMTP = "", strSubject = "", strFromName = "";
private bool blnSendMail;
private NameValueCollection appSettings = ConfigurationManager.AppSettings;
public ExceptionPublisher()
{
}
// Provide implementation of the IPublishException interface
// This contains the single Publish method.
void IExceptionPublisher.Publish(Exception exception, NameValueCollection AdditionalInfo, NameValueCollection ConfigSettings)
{
// Load Config values if they are provided.
if (appSettings != null)
{
if (appSettings["ExceptionFromMail"] != null &&
appSettings["ExceptionFromMail"].Length > 0)
{
strFromMail = appSettings["ExceptionFromMail"];
}
if (appSettings["ExceptionToMail"] != null &&
appSettings["ExceptionToMail"].Length > 0)
{
strToMail = appSettings["ExceptionToMail"];
}
if (appSettings["ServerMail"] != null &&
appSettings["ServerMail"].Length > 0)
{
strSMTP = appSettings["ExceptionServerMail"];
}
if (appSettings["ExceptionSubject"] != null &&
appSettings["ExceptionSubject"].Length > 0)
{
strSubject = appSettings["ExceptionSubject"];
}
if (appSettings["ExceptionFromName"] != null &&
appSettings["ExceptionFromName"].Length > 0)
{
strFromName = appSettings["ExceptionFromName"];
}
if (appSettings["ExceptionSendEmail"] != null &&
appSettings["ExceptionSendEmail"].Length > 0)
{
if (appSettings["ExceptionSendEmail"].ToString() == "on")
blnSendMail = true;
}
}
// Create StringBuilder to maintain publishing information.
StringBuilder strInfo = new StringBuilder();
// Record the contents of the AdditionalInfo collection.
if (AdditionalInfo != null)
{
// Record General information.
strInfo.AppendFormat("{0}General Information{0}", Environment.NewLine);
strInfo.AppendFormat("{0}Additonal Info:", Environment.NewLine);
foreach (string i in AdditionalInfo)
{
strInfo.AppendFormat("{0}{1}: {2}", Environment.NewLine, i, AdditionalInfo.Get(i));
}
}
// Append the exception text
strInfo.AppendFormat("{0}{0}Exception Information{0}{1}", Environment.NewLine, exception.ToString());
strInfo.AppendFormat("{0}{0}---------------------{0}{1}", Environment.NewLine, " ");
// Write the entry to the log file.
using ( FileStream fs = File.Open(m_LogName,
FileMode.Append,FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(strInfo.ToString());
}
}
// send notification email if operatorMail attribute was provided
if (strSMTP.Length > 0 && strSubject.Length > 0 && strToMail.Length > 0 && strFromMail.Length > 0 &&
blnSendMail)
{
System.Net.Mail.MailMessage mlmExceptionMail;
System.Net.Mail.MailAddress mlaExceptionSender, mlaExceptionRecipient;
System.Net.Mail.SmtpClient stcExceptionSmtp;
mlmExceptionMail = new System.Net.Mail.MailMessage();
mlaExceptionSender = new System.Net.Mail.MailAddress(strFromMail, strFromName);
mlaExceptionRecipient = new System.Net.Mail.MailAddress(strToMail);
stcExceptionSmtp = new System.Net.Mail.SmtpClient(strSMTP);
mlmExceptionMail.Body = strInfo.ToString();
mlmExceptionMail.Subject = strSubject;
mlmExceptionMail.IsBodyHtml = true;
mlmExceptionMail.From = mlaExceptionSender;
mlmExceptionMail.To.Add(mlaExceptionRecipient);
stcExceptionSmtp.Send(mlmExceptionMail);
}
}
}
}