自定义日志记录在活动任务SSIS中

时间:2016-04-01 19:30:53

标签: sql-server excel ssis error-logging

我在SSIS包中面临两个问题。我有一个SSIS包有一个活动任务,根据我的要求格式化Excel工作表,并将其保存为另一个文件,modified.xlsx。然后,在我的数据流任务中使用此文件来处理数据并将数据上载到数据库表。 这个包在我的本地系统中运行得很好,但是当我在我的SQL服务器上创建一个预定作业来运行这个包时,它失败并显示一般错误消息" Microsoft(R)SQL Server执行包实用程序版本11.0.5058.0 for 64 -bit版权所有(C)Microsoft Corporation。版权所有。开始时间:12:06:55 PM错误:2016-04-01 12:06:57.06代码:0x00000001源:脚本任务描述:调用目标抛出了异常。结束错误DTExec:程序包执行返回DTSER_FAILURE(1)。开始于:12:06:55 PM完成时间:下午12:06:57经过:1.563秒。包执行失败。步骤失败。"

要获得更详细的错误消息,我尝试为活动任务设置日志记录。 我将日志配置为将事件的日志条目写入CSV文件,如下面的屏幕截图所示。 enter image description here enter image description here

我启用了包的日志记录并检查了个人任务。在活动任务中,我添加了Dts.Log("",0,字节);跟踪任何异常(如果有的话)也记录每个步骤。

public partial class ScriptMain:Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
 byte[] bytes = new byte[0];
    public void Main()
            {
                LogMessages("");

                LogMessages("Update Bug package execution started at :: " + DateTime.Now.ToLongTimeString());
                LogMessages("Loading package configuration values to local variables.");

                FileName = Convert.ToString(Dts.Variables["User::ExcelFileName"].Value);
                SourceFileLocation = Convert.ToString(Dts.Variables["User::SourceFileLoc"].Value);

                SourceFileName = Path.Combine(SourceFileLocation, FileName);
                saveLoc = Path.Combine(SourceFileLocation, "ModifiedExcel.xlsx");


                var excel = new Excel.Application();
                var workbook = excel.Workbooks.Open(SourceFileName);
                try
                {

                    foreach (Excel.Worksheet tempSheet in workbook.Worksheets)
                    {
                        LogMessages("For loop to check sheet names");
                        if (((Excel.Worksheet)(tempSheet)).Name.Contains("Test"))
                        {
                            if (File.Exists(saveLoc))
                            {
                                File.Delete(saveLoc);
                            }

                            //File.Create(saveLoc);
                            tempSheet.Select();
                            workbook.SaveAs(saveLoc);
                        }

                        System.Runtime.InteropServices.Marshal.ReleaseComObject(tempSheet);
                    }

                    workbook.Save();
                    workbook.Close();
                    excel.Quit();
                    LogMessages("Quit Excel sheet");

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
                    LogMessages("Release excel objects");
                }
                catch(Exception ex)
                {
                    LogMessages("Exception: " + ex.InnerException);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
                }


                Dts.TaskResult = (int)ScriptResults.Success;
            }

            #region ScriptResults declaration
            /// <summary>
            /// This enum provides a convenient shorthand within the scope of this class for setting the
            /// result of the script.
            /// 
            /// This code was generated automatically.
            /// </summary>
            enum ScriptResults
            {
                Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
                Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
            };
            #endregion

            #region Log messages to package log files/table.
            public void LogMessages(string strLogMsg)
            {
                Dts.Log(strLogMsg, 0, bytes);
            }
            #endregion
}

但是当我运行包时,日志文件不会更新。日志文件只包含以下内容:

字段:事件,计算机,运算符,源,sourceid,executionid,starttime,endtime,datacode,databytes,message

有人可以帮我理解我在这里记录的日志吗?此外,SQL服务器中的作业失败可能是什么问题?

1 个答案:

答案 0 :(得分:4)

为什么不记录?

这是有趣的部分,就像我多年来在处理SSIS时所能说的一样。 Dts.Log非常没用,至少如果你希望它出现在SSIS内置的Logging工具中。

相反,将Dts.Log调用更改为Dts.Events。 Fire ,例如

bool fireAgain = false;
Dts.Events.FireInformation(0, "This gest logged", "My long description here", string.Empty, 0, ref fireAgain);

然后,在上面的详细信息标签中,确保您已检查OnInformation事件(这也假设您已将程序包配置为全部跟踪)

enter image description here

最后,如果您实际上没有单击“提供者和日志”选项卡中的按钮,它将不会记录到表中

为什么不起作用?

程序包无法运行,因为您正在处理Excel,并且错误消息指定您正在以64位模式运行。

  

Microsoft(R)SQL Server执行包实用程序版本11.0.5058.0(适用于64位)

除非你已经做了一些明确让64位Excel在这台服务器上运行的东西,否则它不会起作用。相反,在SQL代理作业中,您需要指定此任务以32位模式运行。

另见

Why does my ODBC connection fail when running an SSIS load in Visual Studio but not when running the same package using Execute Package Utility