用于发送电子邮件的 SSIS onError 脚本任务 - 包括异常详细信息
SSIS onError script task to send email - include exception details
我有一个 SSIS 程序包,我在其中针对 OnError 事件创建了一个脚本任务,以发送电子邮件提醒用户发生了错误。
这工作正常,但我想做的是在我的电子邮件正文中包含导致事件处理程序触发的异常消息。如何访问脚本任务中的异常?
当前脚本正文:
/// <summary>
/// This method is called when this script task executes in the control flow.
/// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
/// To open Help, press F1.
/// </summary>
public void Main()
{
try
{
string mailBody = "<p>XXXX package has failed.</p><p>Please investigate.</p>";
string mailFrom = Dts.Variables["MailFrom"].Value.ToString();
string errorMailTo = Dts.Variables["ErrorMailTo"].Value.ToString();
string smtpConnectionString = (string)(Dts.Connections["SMTPConnectionManager"].AcquireConnection(Dts.Transaction));
string smtpServer = smtpConnectionString.Split(new char[] { '=', ';' })[1];
var smtpClient = new SmtpClient(smtpServer);
var message = new MailMessage(mailFrom, errorMailTo, mailSubject, mailBody) { IsBodyHtml = true };
// TODO append exception message to the mail body.
smtpClient.Send(message);
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
string message = ex.Message;
if (ex.InnerException != null)
{
message = message + " " + ex.InnerException.Message;
}
Dts.Log("Error email sending failure - " + message, 0, new byte[0]);
Dts.TaskResult = (int)ScriptResults.Failure;
throw;
}
}
这通常存储在@[System::ErrorDescription] 变量中,您需要将其映射为只读才能访问。
您也可以使用@[System::ErrorCode],但 SSIS 错误代码通常用处不大。
我有一个 SSIS 程序包,我在其中针对 OnError 事件创建了一个脚本任务,以发送电子邮件提醒用户发生了错误。
这工作正常,但我想做的是在我的电子邮件正文中包含导致事件处理程序触发的异常消息。如何访问脚本任务中的异常?
当前脚本正文:
/// <summary>
/// This method is called when this script task executes in the control flow.
/// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
/// To open Help, press F1.
/// </summary>
public void Main()
{
try
{
string mailBody = "<p>XXXX package has failed.</p><p>Please investigate.</p>";
string mailFrom = Dts.Variables["MailFrom"].Value.ToString();
string errorMailTo = Dts.Variables["ErrorMailTo"].Value.ToString();
string smtpConnectionString = (string)(Dts.Connections["SMTPConnectionManager"].AcquireConnection(Dts.Transaction));
string smtpServer = smtpConnectionString.Split(new char[] { '=', ';' })[1];
var smtpClient = new SmtpClient(smtpServer);
var message = new MailMessage(mailFrom, errorMailTo, mailSubject, mailBody) { IsBodyHtml = true };
// TODO append exception message to the mail body.
smtpClient.Send(message);
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
string message = ex.Message;
if (ex.InnerException != null)
{
message = message + " " + ex.InnerException.Message;
}
Dts.Log("Error email sending failure - " + message, 0, new byte[0]);
Dts.TaskResult = (int)ScriptResults.Failure;
throw;
}
}
这通常存储在@[System::ErrorDescription] 变量中,您需要将其映射为只读才能访问。
您也可以使用@[System::ErrorCode],但 SSIS 错误代码通常用处不大。