SSIS: How to read error messages from a console app and send them by email
SSIS: How to read error messages from a console app and send them by email
Let’s say you have a SSIS package that runs a console app, everything works fine, but one day, the console app fails, and the SSIS package fails with a non helpful error message. You want to know what was the error, what data was involved and you want it be email
Works with
- SSIS project in Visual Studio 2015
- Simple console app in .NET 4.8
Steps
Console app
- Create a console app for C#
- Replace the code in Program.cs with this:
In this program we’re doing the followingclass Program { static int Main(string[] args) { try { string value = "a"; //Output to StdOutput variable Console.WriteLine("Value to convert:" + value); int i = int.Parse(value); return 0; //In console apps, normally returning 0 indicates success } catch (Exception ex) { //Output to StdError variable Console.Error.WriteLine(ex.Message); return -1; //Returning any value different than 0 might indicate failure or some other condition } } }
- Writing data to the console
- Deliberately throwing an error by trying to cast a letter to a number
- Catching the exception, writing the error to the console and returning -1
- Build the project
SSIS
-
Create an SSIS package
-
Add the following variables
- ReturnCode (int32) - Returns the code number from the Main function in the console app, this number could be 0 for success or -1 for error;
- StdError (String) - Stores anything printed out with Console.Error.WriteLine(…) in the console app
- StdOutput - String - Stores anything printed out with Console.WriteLine(…) in the console app
-
and add an Execute Process Task to the control Flow
-
Right click the element and select Properties
-
Configure properties like this:
- Executable: Browse the consola app.exe, should be something like
C:\...\ConsoleApplication1\bin\debug\ConsoleApplication1.exe
- ExecValueVariable: ReturnCode variable
- FailPackageOnFailure: False
- FailParentOnFailure: False
- StandardErrorVariable: StdError variable
- StandardOutputVriable: StdOutput variable
- WorkingDirectory: The folder of
ConsoleApplication1.exe
, should be something likeC:\...\ConsoleApplication1\bin\debug\
Changes are highlighted: - Executable: Browse the consola app.exe, should be something like
-
You can also directly use the task editor
- Add a Send Mail Task element
- Connect the Execute Process Task to the Send Mail Task
- Double click the green arrow
- In Constraint options, set Value = “Failure”
- Click OK
- Double click Send Mail Task and configure it like this:
- SMTP Connection - click <New connection> and configure your SMTP manager
- From - An email account to send emails
- To - A destination account
- Specify a subject
- Message Source - Don’t write anything
- In the same window, click Expressions
- Add an expression for MessageSource
"Return Code:" + (DT_WSTR, 4)@[User::ReturnCode] + "\r\n" + "Error:" + @[User::StdError] + "\r\n" + "Output:" + @[User::StdOutput]
- Click OK
- Run the package
- Execute Process Task should fail
- Send Mail Task should succeed
- Check you email
Comments
Post a Comment