How to set up OnError Event for Parent and Child P

2019-08-03 17:14发布

问题:

I am using 2016 version and have a master ETL_Extract package where I am executing three Child packages(ABC,DEF,XYZ) using Execute Package Task. I would like to capture the System::Error_Description for any error occurs in any of these child packages and has to be notify as an email message through parent package.

I am using OnError Event on Child package with below script to capture the "System::Error_Description" using script task on OnError event for Child as shown below:

Using below system variables as read only in Child Script task:

System::PackageName
System::SourceName
System::ErrorDescription


using System;
using System.Data;
using System.IO;
// build our the error message
    string ErrorMessageToPassToParent = "Package Name:  " + Dts.Variables["System::PackageName"].Value.ToString() + Environment.NewLine + Environment.NewLine +
        "Step Failed On:  " + Dts.Variables["System::SourceName"].Value.ToString() + Environment.NewLine + Environment.NewLine +
        "Error Description:  " + Dts.Variables["System::ErrorDescription"].Value.ToString();


// Populate collection of variables.
// This will include parent package variables.
Variables vars = null;
Dts.VariableDispenser.GetVariables(ref vars);

// Lock the variables. 
Dts.VariableDispenser.LockForWrite("User::OnError_Description_FromChild");

Dts.VariableDispenser.GetVariables(ref vars);
vars["User::OnError_Description_FromChild"].Value = ErrorMessageToPassToParent;

vars.Unlock();

Then in my parent package ETL_Extract I declared string variable 'OnError_Description_FromChild' and set up a OnError Event for ETL_Extract package.

In Parent package Script task I am using 'OnError_Description_FromChild' as read only variable. Below is parent package script.

  // get the variable from the parent package for the error
    string ErrorFromChildPackage = Dts.Variables["User::OnError_ErrorDescription_FromChild"].Value.ToString();

    // do a check if the value is empty or not (so we know if the error came from the child package or occurred in the parent package itself
    if (ErrorFromChildPackage.Length > 0)
    {
        // Then raise the error that was created in the child package
        Dts.Events.FireError(0, "Capture Error From Child Package Failure",
                ErrorFromChildPackage   
                , String.Empty, 0);

    } // end if the error length of variable is > 0

I am C# beginner but have got few references from stackoverflow to code the above logic.There are few things happening here, when I am executing parent package with some faulty conditions on one child package.

First the Error occurred on child package inside foreach loop container and child package script task executes multiple times.

Second when script task gets completed on child package , it didn't triggered On Error event for my Parent package whereas parent package also gets failed. I am not sure why OnError event not firing on Parent package. I have been looking for information on how to access child package variable in the parent package.