Tải bản đầy đủ - 0 (trang)
15-12. Logging to a Centralized Framework When Using SSIS Script Tasks and Components

15-12. Logging to a Centralized Framework When Using SSIS Script Tasks and Components

Tải bản đầy đủ - 0trang

Chapter 15 ■ Logging and Auditing


Use .NET from inside the Script task or Component to call the stored procedure that carries out logging.


Create an SSIS package.


Create an ADO.NET connection manager named CarSales_Logging_ADONET that

connects to the database CarSales_Logging—unless it exists already, of course.


Add a Script task. Double-click to Edit.


Set the script language to Microsoft Visual Basic 2010 and click the Edit Script button.


At the appropriate point(s) in your script add the following code to write to the log

table (C:\SQL2012DIRecipes\CH15\CentralisedSSISLogging.vb):

Dim StartTime As Date = Now()

Dim cnMgr As SqlClient.SqlConnection

cnMgr = DirectCast(Dts.Connections("CarSales_ADONET") 

.AcquireConnection(Dts.Transaction), SqlClient.SqlConnection)

Dim cmd As New SqlClient.SqlCommand

cmd.CommandText = "Log.pr_LogEvents"

cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.AddWithValue("Process", DbType.String).Value = 


cmd.Parameters.AddWithValue("Step", DbType.String).Value = 


cmd.Parameters.AddWithValue("StartTime", DbType.DateTime).Value = StartTime

cmd.Parameters.AddWithValue("Comments", DbType.String).Value = 

"This seems to have worked!"



Dts.TaskResult = ScriptResults.Success


Close the script window and click OK to complete your modifications.

How It Works

One slightly specific requirement in logging SSIS packages is detailed logging from Script tasks. Certainly, if the task

is simple, then merely adding the three event handlers (OnPreExecute, OnPostExecute, and OnError) will suffice.

However, a few tweaks may be required to push this a little further and to track events—and errors—more fully.

Should your Script task be complex or iterative, you may want to log process events as part of the task

execution. You will need the following:

An ADO.NET connection manager to the logging database.

The logging stored procedure (Log.pr_LogEvents), as given in Recipe 15-10.

The logging table (log.EventDetail), as defined in Recipe 15-10.



Chapter 15 ■ Logging and Auditing

Of course, if you are logging multiple events from inside the Script task, then for all subsequent events

you will only need to reset the Comments parameter and add cmd.ExecuteNonQuery(). You will need to add

ImportsSystem.Data.SqlClient to the script file. You will also need to add the most accurate and descriptive

comment for the event that you can, as you may need to understand it in a log table one day.

There are a few differences if you wish to write to the central log table from an SSIS Script component.

Specifically the connection manager is defined for the task, and then “acquired” inside the script.

You will again need an ADO.NET connection manager to the logging database, the logging stored procedure

(Log.pr_LogEvents), and the logging table (log.EventDetail).

Assuming that these are in place, carry out the following steps:


Double-click to edit the Script component.


Click Connection Managers and then click Add.


Select the ADO.NET connection manager that you are using for logging from the list.

The dialog box should look like Figure 15-6.

Figure 15-6.  Logging from a Script task



Chapter 15 ■ Logging and Auditing


Click Script and then Edit Script.


Add the following to the body of the code:

Public Overrides Sub AcquireConnections(ByVal Transaction As Object)

SQLMgr = Me.Connections.LogConnection

cnMgr = CType(SQLMgr.AcquireConnections(Nothing), 


End Sub


Add the logging code (as just described) for the Script task.

If you have added an OnError event handler to the SSIS script task, it would be particularly useful to ensure

that any error messages raised during script execution were logged accurately. This can be done by trapping

any errors, and then firing an error event—which passes the error message to the task error handler. It could be

something like this:


'Your code here

Catch ex As Exception

' Error code and description passed to handler

Dts.Events.FireError(20, "", ex.Message, "", False)

End Try

This way, if an error is encountered, the OnError SSIS event is triggered and the specific error message is

logged to the central error table—without having to write complex logging code as part of the Script component.

■■Note Even if it is easy enough to copy and paste the Execute SQL tasks that comprise the logging infrastructure

triggered by the event handlers, it can rapidly become a little wearing. Another valid approach is to define the three

Execute SQL logging tasks for the OnPreExecute, OnPostExecute, and OnError tasks—but at package level—that

is, click anywhere inside the Control Flow pane before clicking the Event Handler tab. This means that any event

handler set here will fire for every task in the package. However, you then add a degree of control by setting the

DisableEventHandler property for the package to False, and set it to True only for the tasks that you want to log.

You will, however, still have to define task-scoped variables for each of the tasks that will be logged.

As a final point, copying and pasting T-SQL logging and error-handling code into dozens of stored

procedures can rapidly become as frustrating as it is tedious. So it is probably worth reminding ourselves that

SSMS allows you to use stored procedure templates when creating sprocs. The trick is to locate the directory

where these are stored.



Chapter 15 ■ Logging and Auditing

On 32-bit Windows, it is (on my 32-bit workstation, at least):

C:\Program Files\Microsoft SQL


On 64-bit Windows, it is (on my 64-bit workstation, at least):

C:\Program Files (x86)\Microsoft SQL


In either case, you can use one of the existing directories or create your own alongside those that you find

there. You can then copy an SQL file containing your template structure (with a suitable name) into the directory.

Once this is done, all you have to do is to press Ctrl+Alt+T or click View Template Explorer to display the

template window, and then open the template you previously created as a model for your new procedure.

If you really want to go overboard, you can set the template as the default New Stored Procedure template by

pasting the contents of your logging template into the following file:

C:\Program Files\Microsoft SQL Server\110\Tools\Binn\VSShell\Common7\IDE\ 

SqlWorkbenchProjectItems\Sql\Stored Procedure\ Create Stored Procedure (New Menu).sql

In SSMS, expand Programmability/Stored Procedures for a database. Right-click and select New Stored

Procedure. You will have your template loaded ready to be extended.

15-13. Logging to a Text or XML File from T-SQL


You want to log process information to a text or XML file rather than to an SQL Server table.


Create a CLR-based stored procedure to log information to disk, as follows.


First, you need a simple CLR routine to output the log data to disk. This is done by

creating a CLR stored procedure named WriteToDisk, using the following C# code

(for an overview of how to create and load a CLR routine, see Recipe 10-21):

using System;

using System.Diagnostics;

using System.IO;

public class DiskWriter



public static void WriteToDisk(string theData, string theFile)


File.WriteAllText(theFile, theData);





Chapter 15 ■ Logging and Auditing


Once you have deployed and installed the CLR stored procedure, you can use it to

write to a text file. This is as then easy as stringing together the elements you wish to

log and passing them to the CLR stored procedure, as the following T-SQL snippet



DECLARE @PackageName VARCHAR(150)



DECLARE,@MachineName VARCHAR(150)

SET PackageName = 'MyPackage'

SET StepName = 'FirstStep'

SET StartTime = CONVERT(VARCHAR(20), @StartTime, 112)

SET MachineName = 'MyPC'

SET @OUTPUT = @PackageName + ',' + @StepName + ',' + @StartTime + ',' + @MachineName

EXECUTE dbo.WriteToDisk @OUTPUT,'C:\SQL2012DIRecipes\CH13\AA_log.Csv'

How It Works

Should you prefer to write status information to a file outside SQL Server (as text or XML), then it can be done

using fairly “classic” techniques. There could be several reasons for you wanting to do this:

Logging to a file reduces database overhead—at the slight cost of running a CLR function.

You can log data to a different server easily, further reducing load on a main SQL Server.

You prefer log data to be kept outside a database.

Should there be a fatal database problem, you still have log data.

First, you will need a CLR-based stored procedure that takes two input parameters:

The row of data to be written to the file.

The full path of the file to which data will be written.

This procedure consists of just one line that uses the WriteAllText function to add a line to the file that is

specified. Once this CLR assembly is compiled and deployed to SQL Server and the assembly created, it is called

just like any other stored procedure.

Writing an XML log can require some preparation of the XML. Here I will format data taken from a log table

and export to XML using the same format as that used by SSIS:






ERROR_LOG_ID AS 'record/event',

CREATEDATE AS 'record/message',

CREATEUSERNAME AS 'record/computer'



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

15-12. Logging to a Centralized Framework When Using SSIS Script Tasks and Components

Tải bản đầy đủ ngay(0 tr)