다음을 통해 공유


사용자 지정 로그 공급자 코딩

적용 대상: Azure Data Factory의 SQL Server SSIS Integration Runtime

기본 클래스에서 LogProviderBase 상속되는 클래스를 만들고 클래스에 DtsLogProviderAttribute 특성을 적용한 후에는 기본 클래스의 속성 및 메서드 구현을 재정의하여 사용자 지정 기능을 제공해야 합니다.

사용자 지정 로그 공급자의 작업 샘플은 사용자 지정 로그 공급자에 대한 사용자 인터페이스 개발을 참조하세요.

로그 공급자 구성

로그 공급자 초기화

연결 컬렉션 및 이벤트 인터페이스에 대한 참조를 캐시하도록 메서드를 재정 InitializeLogProvider 의합니다. 이렇게 캐시된 참조는 나중에 로그 공급자의 다른 메서드에서 사용할 수 있습니다.

ConfigString 속성 사용

디자인 타임에 로그 공급자는 구성 열에서 구성 정보를 받습니다. 이 구성 정보는 로그 공급자의 ConfigString 속성에 해당합니다. 기본적으로 이 열에는 문자열 정보를 검색할 수 있는 입력란이 들어 있습니다. Integration Services에 포함된 대부분의 로그 공급자는 이 속성을 사용하여 공급자가 외부 데이터 원본에 연결하는 데 사용하는 연결 관리자의 이름을 저장합니다. 로그 공급자가 ConfigString 속성을 사용하는 경우에는 Validate 메서드를 사용하여 이 속성의 유효성을 검사하고 속성이 올바르게 설정되어 있는지 확인합니다.

로그 공급자 유효성 검사

메서드를 재정의 Validate 하여 공급자가 올바르게 구성되었고 실행할 준비가 되었는지 확인합니다. 일반적으로 유효성 검사의 최소 수준은 올바르게 설정되었는지 확인하는 ConfigString 것입니다. 로그 공급자가 메서드에서 Validate 반환 Success 될 때까지 실행을 계속할 수 없습니다.

다음 코드 예에서는 연결 관리자 이름이 지정되어 있고, 연결 관리자가 패키지에 있으며, 연결 관리자가 Validate 속성에서 파일 이름을 반환하는지 확인하는 ConfigString의 구현을 보여 줍니다.

public override DTSExecResult Validate(IDTSInfoEvents infoEvents)  
{  
    if (this.ConfigString.Length == 0 || connections.Contains(ConfigString) == false)  
    {  
        infoEvents.FireError(0, "MyTextLogProvider", "The ConnectionManager " + ConfigString + " specified in the ConfigString property cannot be found in the collection.", "", 0);  
        return DTSExecResult.Failure;  
    }  
    else  
    {  
        string fileName = connections[ConfigString].AcquireConnection(null) as string;  
  
        if (fileName == null || fileName.Length == 0)  
        {  
            infoEvents.FireError(0, "MyTextLogProvider", "The ConnectionManager " + ConfigString + " specified in the ConfigString property cannot be found in the collection.", "", 0);  
            return DTSExecResult.Failure;  
        }  
    }  
    return DTSExecResult.Success;  
}  
Public Overrides Function Validate(ByVal infoEvents As IDTSInfoEvents) As DTSExecResult  
    If Me.ConfigString.Length = 0 Or connections.Contains(ConfigString) = False Then  
        infoEvents.FireError(0, "MyTextLogProvider", "The ConnectionManager " + ConfigString + " specified in the ConfigString property cannot be found in the collection.", "", 0)  
        Return DTSExecResult.Failure  
    Else   
        Dim fileName As String =  connections(ConfigString).AcquireConnectionCType(as string, Nothing)  
  
        If fileName = Nothing Or fileName.Length = 0 Then  
            infoEvents.FireError(0, "MyTextLogProvider", "The ConnectionManager " + ConfigString + " specified in the ConfigString property cannot be found in the collection.", "", 0)  
            Return DTSExecResult.Failure  
        End If  
    End If  
    Return DTSExecResult.Success  
End Function  

로그 공급자 지속

일반적으로 연결 관리자에 대한 사용자 지정 지속성을 구현할 필요가 없습니다. 사용자 지정 지속성은 개체의 속성이 복잡한 데이터 형식을 사용하는 경우에만 필요합니다. 자세한 내용은Integration Services 사용자 지정 개체 개발을 참조하세요.

로그 공급자를 사용하여 로깅

모든 로그 공급자에서 재정의해야 하는 세 가지 런타임 메서드가 있습니다. OpenLogLogCloseLog

Important

단일 패키지의 유효성 검사 및 실행 중에 메서드와 CloseLog 메서드가 OpenLog 두 번 이상 호출됩니다. 사용자 지정 코드가 로그의 다음 열기 및 닫기로 인해 이전 로그 항목을 덮어쓰지 않도록 합니다. 테스트 패키지에서 유효성 검사 이벤트를 기록하도록 선택한 경우 표시되는 첫 번째 로깅 이벤트는 OnPreValidate입니다. 표시되는 첫 번째 로깅 이벤트가 PackageStart이면 초기 유효성 검사 이벤트를 덮어씁니다.

로그 열기

대부분의 로그 공급자는 파일 또는 데이터베이스와 같은 외부 데이터 원본에 연결하여 패키지 실행 중에 수집된 이벤트 정보를 저장합니다. 런타임의 다른 개체와 마찬가지로 외부 데이터 원본에 연결하는 작업은 일반적으로 연결 관리자 개체를 사용하여 수행됩니다.

OpenLog 메서드는 패키지 실행이 시작될 때 호출됩니다. 외부 데이터 원본에 대한 연결을 설정하려면 이 메서드를 재정의합니다.

다음 예제 코드에서는 OpenLog 실행 중에 텍스트 파일을 쓰기용으로 여는 로그 공급자를 보여 줍니다. 속성에 지정된 ConfigString 연결 관리자의 메서드를 호출 AcquireConnection 하여 파일을 엽니다.

public override void OpenLog()  
{  
    if(!this.connections.Contains(this.ConfigString))  
        throw new Exception("The ConnectionManager " + this.ConfigString + " does not exist in the Connections collection.");  
  
    this.connectionManager = connections[ConfigString];  
    string filePath = this.connectionManager.AcquireConnection(null) as string;  
  
    if(filePath == null || filePath.Length == 0)  
        throw new Exception("The ConnectionManager " + this.ConfigString + " is not a valid FILE ConnectionManager");  
  
    //  Create a StreamWriter to append to.  
    sw = new StreamWriter(filePath,true);  
  
    sw.WriteLine("Open log" + System.DateTime.Now.ToShortTimeString());  
}  
Public Overrides  Sub OpenLog()  
    If Not Me.connections.Contains(Me.ConfigString) Then  
        Throw New Exception("The ConnectionManager " + Me.ConfigString + " does not exist in the Connections collection.")  
    End If  
  
    Me.connectionManager = connections(ConfigString)  
    Dim filePath As String =  Me.connectionManager.AcquireConnectionCType(as string, Nothing)  
  
    If filePath = Nothing Or filePath.Length = 0 Then  
        Throw New Exception("The ConnectionManager " + Me.ConfigString + " is not a valid FILE ConnectionManager")  
    End If  
  
    '  Create a StreamWriter to append to.  
    sw = New StreamWriter(filePath,True)  
  
    sw.WriteLine("Open log" + System.DateTime.Now.ToShortTimeString())  
End Sub  

로그 항목 작성

Log 메서드는 패키지의 개체가 이벤트 인터페이스 중 하나에서 Fire<event> 메서드를 호출하여 이벤트를 발생시킬 때마다 호출됩니다. 각 이벤트는 컨텍스트에 대한 정보와 일반적으로 설명 메시지와 함께 발생합니다. 그러나 메서드에 대한 모든 호출에 Log 모든 메서드 매개 변수에 대한 정보가 포함되지는 않습니다. 예를 들어 이름이 자체 설명인 일부 표준 이벤트는 MessageText를 제공하지 않으며 DataCode 및 DataBytes는 선택적 추가 정보를 위한 것입니다.

다음 코드 예제에서는 메서드를 Log 구현하고 이전 섹션에서 연 스트림에 이벤트를 씁니다.

public override void Log(string logEntryName, string computerName, string operatorName, string sourceName, string sourceID, string executionID, string messageText, DateTime startTime, DateTime endTime, int dataCode, byte[] dataBytes)  
{  
    sw.Write(logEntryName + ",");  
    sw.Write(computerName + ",");  
    sw.Write(operatorName + ",");  
    sw.Write(sourceName + ",");  
    sw.Write(sourceID + ",");  
    sw.Write(messageText + ",");  
    sw.Write(dataBytes + ",");  
    sw.WriteLine("");  
}  
Public Overrides  Sub Log(ByVal logEnTryName As String, ByVal computerName As String, ByVal operatorName As String, ByVal sourceName As String, ByVal sourceID As String, ByVal executionID As String, ByVal messageText As String, ByVal startTime As DateTime, ByVal endTime As DateTime, ByVal dataCode As Integer, ByVal dataBytes() As Byte)  
    sw.Write(logEnTryName + ",")  
    sw.Write(computerName + ",")  
    sw.Write(operatorName + ",")  
    sw.Write(sourceName + ",")  
    sw.Write(sourceID + ",")  
    sw.Write(messageText + ",")  
    sw.Write(dataBytes + ",")  
    sw.WriteLine("")  
End Sub  

로그 닫기

CloseLog 이 메서드는 패키지 실행이 끝날 때, 패키지의 모든 개체가 실행을 완료한 후 또는 오류로 인해 패키지가 중지될 때 호출됩니다.

다음 코드 예제에서는 메서드 중에 열린 파일 스트림을 닫는 메서드의 CloseLog 구현을 OpenLog 보여 줍니다.

public override void CloseLog()  
{  
    if (sw != null)  
    {  
        sw.WriteLine("Close log" + System.DateTime.Now.ToShortTimeString());  
        sw.Close();  
    }  
}  
Public Overrides  Sub CloseLog()  
    If Not sw Is Nothing Then  
        sw.WriteLine("Close log" + System.DateTime.Now.ToShortTimeString())  
        sw.Close()  
    End If  
End Sub  

참고 항목

사용자 지정 로그 공급자 만들기
사용자 지정 로그 공급자에 대한 사용자 인터페이스 개발