在C#.Net 中调用SSIS 开发的Package,并用日志记录Package执行中的错误和异常信息

using System;
using System.Collections.Generic;
using System.Text;
using DTSRuntime = Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Server;
using System.Data;
using System.Xml;
using System.Xml.XPath;

namespace SSISRUN
{
    class MyEventListener : DTSRuntime.DefaultEvents
    {
        //自己引用的自定写日志的方法
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public override bool OnError(DTSRuntime.DtsObject source, int errorCode, string subComponent,
          string description, string helpFile, int helpContext, string idofInterfaceWithError)
        {
            //记录Package在运行中出现的错误,以便调试
            log.Info("错误代码:" + source + "<BR>" + "错误组件:" + subComponent + "<BR>" + "错误描叙:" + description + "<BR>");
            return false;
        }
    }
    public class RunSSIS
    {
         //自己引用的自定写日志的方法
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static  string  Exepackage(string pkgSourse, string ConfigSourse)
        {
            string theResult = "执行SSIS包失败!";
            MyEventListener eventListener = new MyEventListener();
            DTSRuntime.Application app = new DTSRuntime.Application();
            DTSRuntime.Package pkg = app.LoadPackage(pkgSourse, null);//读取包文件(dtsx)
        //DTSRuntime.Package pkg = app.LoadFromSqlServer("ExamplePackage","server_name", "sa", "your_password", null); 从SQL Server加载包
            pkg.ImportConfigurationFile(ConfigSourse);//读取包的配置备件(dtsxConfig)
            //   pkg.Variables["xxx"].Value =xxx 也可用这种方式给Package的变量赋值
            log.Info("开始执行包");
            DTSRuntime.DTSExecResult result = pkg.Execute(null, null, eventListener, null, null);
            log.Info("执行包结果:" + result);

            if(result.Equals(DTSRuntime.DTSExecResult.Success))
            {
                 theResult = "执行SSIS包成功!";
            }
            return theResult;

        }           
    }
}

原文地址:https://www.cnblogs.com/huangbaixun/p/1203430.html