脚本任务中的日志记录Logging in the Script Task

适用于: SQL Server 是 Azure 数据工厂中的 SSIS Integration Runtime Azure Synapse Analytics (SQL DW)APPLIES TO: yesSQL Server yesSSIS Integration Runtime in Azure Data Factory yesAzure Synapse Analytics (SQL DW)

使用 Integration ServicesIntegration Services 包中的日志记录可以记录预定义的事件或用户定义的消息,从而记录有关执行进度、结果和问题的详细信息,供随后分析时使用。The use of logging in Integration ServicesIntegration Services packages lets you record detailed information about execution progress, results, and problems by recording predefined events or user-defined messages for later analysis. 脚本任务可以使用 Dts 对象的 Log 方法记录用户定义的数据 。The Script task can use the Log method of the Dts object to log user-defined data. 如果启用了日志记录,并且在“配置 SSIS 日志”对话框的“详细信息”选项卡中为日志记录选择了 ScriptTaskLogEntry 事件,则调用一次 方法即可将事件信息存储在为该任务配置的所有日志提供程序中 LogIf logging is enabled, and the ScriptTaskLogEntry event is selected for logging on the Details tab of the Configure SSIS Logs dialog box, a single call to the Log method stores the event information in all the log providers configured for the task.

备注

尽管可以直接从脚本任务执行日志记录,但是您可能希望考虑实现事件,而不是日志记录。Although you can perform logging directly from your Script task, you may want to consider implementing events rather than logging. 如果使用事件,不仅能够启用事件消息的日志记录,而且能够通过默认或用户定义的事件处理程序响应事件。When using events, not only can you enable the logging of event messages, but you can also respond to the event with default or user-defined event handlers.

有关日志记录的详细信息,请参阅 Integration Services (SSIS) 日志记录For more information about logging, see Integration Services (SSIS) Logging.

日志记录示例Logging Example

下面的示例演示通过记录表示已处理的行数的值,从脚本任务进行日志记录。The following example demonstrates logging from the Script task by logging a value that represents the number of rows processed.

Public Sub Main()  
  
    Dim rowsProcessed As Integer = 100  
    Dim emptyBytes(0) As Byte  
  
    Try  
        Dts.Log("Rows processed: " & rowsProcessed.ToString, _  
            0, _  
            emptyBytes)  
        Dts.TaskResult = ScriptResults.Success  
    Catch ex As Exception  
        'An error occurred.  
        Dts.Events.FireError(0, "Script Task Example", _  
            ex.Message & ControlChars.CrLf & ex.StackTrace, _  
            String.Empty, 0)  
        Dts.TaskResult = ScriptResults.Failure  
    End Try  
  
End Sub  
using System;  
using System.Data;  
using Microsoft.SqlServer.Dts.Runtime;  
  
public class ScriptMain  
{  
  
    public void Main()  
        {  
            //  
            int rowsProcessed = 100;  
            byte[] emptyBytes = new byte[0];  
  
            try  
            {  
                Dts.Log("Rows processed: " + rowsProcessed.ToString(), 0, emptyBytes);  
                Dts.TaskResult = (int)ScriptResults.Success;  
            }  
            catch (Exception ex)  
            {  
                //An error occurred.  
                Dts.Events.FireError(0, "Script Task Example", ex.Message + "\r" + ex.StackTrace, String.Empty, 0);  
                Dts.TaskResult = (int)ScriptResults.Failure;  
            }  
  
        }  

}}

外部资源External Resources

另请参阅See Also

Integration Services (SSIS) 日志记录Integration Services (SSIS) Logging