在数据流组件中记录和定义日志条目

适用于:Azure 数据工厂中的 SQL Server SSIS Integration Runtime

自定义数据流组件可使用 PostLogMessage 接口的 IDTSComponentMetaData100 方法将消息发布到现有日志条目。 还可以使用 FireInformation 方法或者 IDTSComponentMetaData100 接口的类似方法向用户显示信息。 但是,此方法会导致引发和处理额外事件的开销,并且迫使用户从冗长的信息性消息中筛选出感兴趣的消息。 可以按如下所述使用自定义日志条目,为组件的用户提供不同标记的自定义日志信息。

注册和使用自定义日志条目

注册自定义日志条目

若要注册组件使用的自定义日志条目,请重写 RegisterLogEntries 基类的 PipelineComponent 方法。 下面的示例注册自定义日志条目并提供名称和说明。

using Microsoft.SqlServer.Dts.Runtime;  
...  
private const string MyLogEntryName = "My Custom Component Log Entry";  
private const string MyLogEntryDescription = "Log entry from My Custom Component ";  
...  
    public override void RegisterLogEntries()  
    {  
      this.LogEntryInfos.Add(MyLogEntryName,  
        MyLogEntryDescription,  
        Microsoft.SqlServer.Dts.Runtime.Wrapper.DTSLogEntryFrequency.DTSLEF_CONSISTENT);  
    }  
Imports Microsoft.SqlServer.Dts.Runtime  
...  
Private Const MyLogEntryName As String = "My Custom Component Log Entry"   
Private Const MyLogEntryDescription As String = "Log entry from My Custom Component "  
...  
Public  Overrides Sub RegisterLogEntries()   
  Me.LogEntryInfos.Add(MyLogEntryName, _  
    MyLogEntryDescription, _  
    Microsoft.SqlServer.Dts.Runtime.Wrapper.DTSLogEntryFrequency.DTSLEF_CONSISTENT)   
End Sub  

DTSLogEntryFrequency 枚举向运行时提示记录事件将采用的频率:

上面的示例使用 DTSLEF_CONSISTENT,因为组件希望每执行一次记录一条。

注册自定义日志项目并将自定义组件的实例添加到数据流设计器图面后,设计器中的“日志记录”对话框在可用日志项目列表中显示名为“我的自定义组件日志项目”的新日志项目

记录到自定义日志条目

注册完自定义日志条目后,组件即可记录自定义消息。 下面的示例在 PreExecute 方法的执行过程中写入自定义日志条目,其中包含组件所使用的 SQL 语句文本。

public override void PreExecute()  
{  
  DateTime now = DateTime.Now;  
  byte[] additionalData = null;  
  this.ComponentMetaData.PostLogMessage(MyLogEntryName,  
    this.ComponentMetaData.Name,  
    "Command Sent was: " + myCommand.CommandText,  
    now, now, 0, ref additionalData);  
}  
Public  Overrides Sub PreExecute()   
  Dim now As DateTime = DateTime.Now   
  Dim additionalData As Byte() = Nothing   
  Me.ComponentMetaData.PostLogMessage(MyLogEntryName, _  
    Me.ComponentMetaData.Name, _  
    "Command Sent was: " + myCommand.CommandText, _  
    now, now, 0, additionalData)   
End Sub  

现在,当用户执行包时,在“日志记录”对话框中选择“我的自定义组件日志项目”后,日志将包含明确标记为“User::My Custom Component Log Entry”的条目。此新日志条目包含 SQL 语句文本、时间戳和开发人员记录的所有其他数据。

另请参阅

Integration Services (SSIS) 日志记录