用于发送电子邮件的SSIS onError脚本任务 - 包括异常详细信息

时间:2015-09-29 08:05:53

标签: c# ssis ssis-2012

我有一个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;
        }   
    }

1 个答案:

答案 0 :(得分:1)

这通常存储在@ [System :: ErrorDescription]变量中,您需要将其映射为只读以进行访问。

您也可以使用@ [System :: ErrorCode],但SSIS错误代码通常不是很有帮助。