애플리케이션 로그에 정보 쓰기

Microsoft SSIS(SQL Server Integration Services) 패키지에 스크립트 태스크를 포함하여 작업을 실행할 수 있습니다. 예를 들어 작업은 Windows 애플리케이션 로그에 변수 정보 컬렉션을 작성할 수 있습니다. Data Flow 작업이 포함된 SSIS 패키지를 만들 수 있습니다. 이 Data Flow 작업에는 행 개수 변환이 포함됩니다. 스크립트 태스크를 사용하여 행 수 변환으로 채워진 데이터를 Windows 애플리케이션 로그에 쓸 수 있습니다.

이 문서에서는 스크립트 태스크를 사용하여 Windows 애플리케이션 로그에 정보를 쓰는 방법을 설명합니다.

원래 제품 버전: SQL Server
원본 KB 번호: 906560

설명

이 예제에서는 SSIS 패키지에서 다음 요소를 만들었다고 가정합니다.

  • Data Flow 작업입니다.
  • 스크립트 태스크입니다.
  • Data Flow 태스크에서 스크립트 태스크로의 커넥터입니다.
  • Data Flow 작업에서는 데이터 흐름에서 행 개수 변환을 만들었습니다.

패키지를 실행하면 행 개수 변환은 Windows 애플리케이션 로그에 쓰려는 행 개수 데이터를 반환합니다.

스크립트 태스크 구성

스크립트 태스크 코드 샘플을 구성하려면 SSIS 디자이너에서 다음 단계를 수행합니다.

  1. 제어 흐름 탭이 활성화되어 있는 동안 디자인 화면을 마우스 오른쪽 단추로 클릭한 다음 변수를 선택합니다. 변수 창이 왼쪽 창에 나타납니다.

  2. 변수 창에서 변수 추가를 선택한 다음 변수 이름을 mycount로 제공합니다. 기본적으로 새 mycount 변수의 데이터 형식은 입니다 Int32.

    참고

    변수 이름에 대한 참조는 대/소문자를 구분합니다.

  3. Data Flow 작업을 두 번 클릭합니다. 활성 창이 Data Flow 탭으로 전환됩니다.

  4. Row Count 변환의 속성을 사용하여 VariableName 속성의 값을 mycount로 설정합니다.

  5. 제어 흐름 탭을 선택한 다음 스크립트 태스크를 두 번 클릭합니다. 스크립트 태스크 편집기 대화 상자가 나타납니다.

  6. 왼쪽 창에서 스크립트 항목을 선택한 다음 ReadOnlyVariables 속성의 값을 다음 값으로 변경합니다.

    PackageName,StartTime,ExecutionInstanceGUID,mycount

    참고

    PackageName, StartTimeExecutionInstanceGUID 항목은 시스템 변수입니다. 이러한 시스템 변수는 패키지 정보를 Windows 애플리케이션 로그에 쓰는 데 사용됩니다.

  7. 스크립트 태스크 편집기 대화 상자에서 스크립트 편집을 선택합니다.

  8. 새 VSTA(Microsoft Visual Studio for Applications) 창이 나타나면 다음 단계를 수행합니다.

    1. 다른 선언 앞에 다음 네임스페이스가 코드에 포함되어 있는지 확인합니다.

      Imports System 
      Imports System.Data
      Imports System.Math
      Imports System.Diagnostics
      Imports Microsoft.SqlServer.Dts.Runtime
      
    2. 다음 코드 샘플을 메서드의 코드 Main() 로 바꿉 있습니다.

      Dim varMyCount As Variable = Dts.Variables("mycount") '
      Dim varPackageName As Variable = Dts.Variables("PackageName")
      Dim varStartTime As Variable = Dts.Variables("StartTime")
      Dim varInstanceID As Variable = Dts.Variables("ExecutionInstanceGUID")
      Dim PackageDuration As Long
      '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ' Event log needs
      '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
      Dim sSource As String
      Dim sLog As String
      Dim sEventMessage As String
      Dim sMachine As String
      '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
      PackageDuration = DateDiff("s", varStartTime.Value, Now())
      sSource = "RowCountReporting from SSIS"
      ' We need the message posted to the Application event log.
      sLog = "Application"
      sEventMessage = "Rows Processed: " & Chr(10) _
      & " case Rows:" + varMyCount.Value().ToString + Chr(10) _
      & "=============================================" & Chr(10) _
      & "The Package: " + varPackageName.Value().ToString _
      & Chr(10) _
      & "Started: " & varStartTime.Value().ToString _
      & Chr(10) _
      & "Current Time:" & System.DateTime.Now _
      & Chr(10) _
      & "=============================================" _
      & Chr(10) _
      & "Package Run Duration in seconds: " & PackageDuration _
      & Chr(10) _
      & "Execution GUID: " & varInstanceID.Value().ToString
      sMachine = "."
      If Not EventLog.SourceExists(sSource, sMachine) Then
      EventLog.CreateEventSource(sSource, slog)
      End If
      Dim ELog As New EventLog(sLog, sMachine, sSource)
      Dim category As Short = 2
      ' ELog.WriteEntry("Write from third source", 4, 777, 2)
      ELog.WriteEntry(sEventMessage, EventLogEntryType.Information, 777, category)
      '###############################
      Dts.TaskResult = ScriptResults.Success
      

    패키지가 성공적으로 실행되면 Windows 애플리케이션 로그에 다음 항목이 표시됩니다.

       Log Name: Application
       Source: RowCountReporting from SSIS
       Date: 12/20/2022 11:21:38 AM
       Event ID: 777
       Task Category: (2)
       Level: Information
       Keywords: Classic
       User: N/A
       Computer: <hostname>
       Description:
       Rows Processed:
       case Rows:0
       =============================================
       The Package: Package
       Started: 12/20/2022 11:21:37 AM
       Current Time:12/20/2022 11:21:38 AM
       =============================================
       Package Run Duration in seconds: 1
       Execution GUID: {9DF22831-E608-47F7-BD62-F9BD3C2F9C77}
       Event Xml:
       <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
       <System>
       <Provider Name="RowCountReporting from SSIS" />
       <EventID Qualifiers="0">777</EventID>
       <Version>0</Version>
       <Level>4</Level>
       <Task>2</Task>
       <Opcode>0</Opcode>
       <Keywords>0x80000000000000</Keywords>
       <TimeCreated SystemTime="2022-12-20T17:21:38.5070621Z" />
       <EventRecordID>122603</EventRecordID>
       <Correlation />
       <Execution ProcessID="41588" ThreadID="0" />
       <Channel>Application</Channel>
       <Computer><hostname>/Computer>
       <Security />
       </System>
       <EventData>
       <Data>Rows Processed:
       case Rows:0
       =============================================
       The Package: Package
       Started: 12/20/2022 11:21:37 AM
       Current Time:12/20/2022 11:21:38 AM
       =============================================
       Package Run Duration in seconds: 1
       Execution GUID: {9DF22831-E608-47F7-BD62-F9BD3C2F9C77}</Data>
       </EventData>
       </Event>
    

참조

EventLog.WriteEntry 메서드(System.Diagnostics)

EventSource 이벤트를 만드는 계측 코드