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

本文关键字:-包 任务 异常 详细信息 脚本 onError 电子邮件 SSIS | 更新日期: 2023-09-27 18:15:15

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

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

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

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