SSIS: Writing to the Windows Event Log

Here’s a quick VB.Net snippet that allows you to write messages to the Windows Event Log from your packages. Nothing to configure here, just change the log message to something appropriate. Obviously, in order to work, the package would have to be run under an account that has permission to write to the event log. Copy the below code into a Script Task, change the message, and you’re ready to go.

' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic
' The ScriptMain class is the entry point of the Script Task.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Diagnostics.EventLog
Imports System.Diagnostics.EventLogEntryType

Public Class ScriptMain

	' The execution engine calls this method when the task executes.
	' To access the object model, use the Dts object. Connections, variables, events,
	' and logging features are available as static members of the Dts class.
	' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
	' 
	' To open Code and Text Editor Help, press F1.
	' To open Object Browser, press Ctrl+Alt+J.

    Public Sub Main()

        Dim eventLog As New Diagnostics.EventLog

        ' If the SSIS event log doesn't exist on the target machine then create it
        If Not eventLog.SourceExists("SSIS") Then
            eventLog.CreateEventSource("SSIS", "Application")
        End If

        ' Create an instance of the EventLog class
        eventLog = New Diagnostics.EventLog()
        ' Specify the source as SSIS
        eventLog.Source = "SSIS"
        ' Add an event log message
        eventLog.WriteEntry("This is a message from a Script Task!", Diagnostics.EventLogEntryType.Information)

        Dts.TaskResult = Dts.Results.Success
    End Sub

End Class

Execute the task and you should find the following message in the event viewer.

SSIS Message in the Windows Event Log


Leave a Reply